在 C# 中解析 Json 字符串时未获取值

发布于 2024-11-06 19:22:48 字数 1637 浏览 0 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏夜暖风 2024-11-13 19:22:48

问题解决了。 (我使用的是 Json.NET)。我注意到 nytimesnews 类的变量应该根据 json 字符串命名。我对代码进行了以下更改,效果非常好。

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

然后在我的主类中我只使用了以下代码

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

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.

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

Then in my main class I just used following code

 // jasonContent is the jason string
 nytimesnews parse = JsonConvert.DeserializeObject<nytimesnews>(jasonContent);
 jasonContent = parse.results[1].body;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文