2011-05-11 5 views
0

nyTimesからjson文字列 "jasonContent"を取得しています。私は以下のコードを書くとき、私は、合計の値を取得し、オフセットが、私は結果に興味がありますが、私は受け付けておりresults.The文字列のために何を取得していないのですができ、この文字列を解析するために、このC#でJson Stringを解析中に値を取得できません

{ 

    "offset": "0", 

    "results": [ 

    { 
     "body": " news goes here", 

     "byline": "By SANA SIWOLOP", 
     "date": "20110511", 
     "title": "SQUARE FEET; Chelsea Piers, a Manhattan Sports Center, Expands Close to 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 Their Head in French Soccer", 
     "url": "http:\/\/www.nytimes.com\/2011\/05\/11\/sports\/soccer\/11iht-SOCCER11.html" 
    }, 
    { 
     "body": "news3 does 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 
} 

のようなものです私は次のコードを書いています

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); 

答えて

1

問題は解決しました。 (私はJson.NETを使用していました)。私は、nytimesnewsクラスの変数にjson文字列に基づいて名前を付けるべきであることに気付きました。私はコードに変更を加え、完全に機能しました。そして、私のメインのクラスで、私はちょうどコード

// jasonContent is the jason string 
nytimesnews parse = JsonConvert.DeserializeObject<nytimesnews>(jasonContent); 
jasonContent = parse.results[1].body; 
次使用

public class nytimesnews 
{ 
     // name of these variables are just like the 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; } 
} 

関連する問題