在 C# 中解析 Json 字符串时未获取值
我从 nyTimes 获取 json 字符串“jasonContent”。当我编写以下代码时,我可以获得总计和偏移量的值,但我对结果感兴趣,但我没有得到任何结果。我收到的字符串是这样的
{
"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);
I am getting the json string "jasonContent" from nyTimes. When I write the following code I can get the values of total and offset but i am interested in results,but I am getting nothing for results.The string I am receiving is something like this
{
"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
}
For parsing this string I am writing the 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题解决了。 (我使用的是 Json.NET)。我注意到 nytimesnews 类的变量应该根据 json 字符串命名。我对代码进行了以下更改,效果非常好。
然后在我的主类中我只使用了以下代码
The problem is solved. (I was using Json.NET). I noticed that the variables of the nytimesnews class should be named according to the json string. I made following changes to the code and it worked perfectly.
Then in my main class I just used following code