Not geting value while Parsing Json String in C# -


i getting json string "jasoncontent" nytimes. when write following code can values of total , offset interested in results,but getting nothing results.the string receiving

{    "offset": "0",     "results": [      {       "body": " news goes here",        "byline": "by sana siwolop",       "date": "20110511",       "title": "square feet; chelsea piers, manhattan sports center, expands close home",       "url": "http:\/\/www.nytimes.com\/2011\/05\/11\/realestate\/commercial\/chelsea-piers-a-manhattan-sports-center-expands-close-to-home.html"     },     {       "body": "news 2 goes here",       "byline": "by rob hughes",       "date": "20110511",       "title": "on soccer; racial politics rear head in french soccer",       "url": "http:\/\/www.nytimes.com\/2011\/05\/11\/sports\/soccer\/11iht-soccer11.html"     },     {       "body": "news3 here",       "byline": "by richard sandomir",       "date": "20110511",       "title": "gus johnson joins fox sports",       "url": "http:\/\/www.nytimes.com\/2011\/05\/11\/sports\/gus-johnson-joins-fox-sports.html"     },],"tokens": [  "sports" ],   "total": 152539 } 

for parsing string writing following code

 public class nytimesnews {     public string offset { get; set; }     public resultobject news2;     public string total { get; set; } }  public class resultobject {     public results[] news; }  public class results {     public string body { get; set; }     public string byline { get; set; }     public string date { get; set; }     public string title { get; set; }     public string url { get; set; } }   nytimesnews parse = jsonconvert.deserializeobject<nytimesnews>(jasoncontent); 

the problem solved. (i using json.net). noticed variables of nytimesnews class should named according json string. made following changes code , worked perfectly.

 public class nytimesnews  {        // name of these variables  data tags in json string        public string offset { get; set; }              public result[] results;        public string total { get; set; }  }   public class results  {        public string body { get; set; }        public string byline { get; set; }        public string date { get; set; }        public string title { get; set; }        public string url { get; set; }  } 

then in main class used following code

 // jasoncontent jason string  nytimesnews parse = jsonconvert.deserializeobject<nytimesnews>(jasoncontent);  jasoncontent = parse.results[1].body; 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -