使用 system.web.script.serialilization 或使用 json.net 解析 Json

发布于 2024-11-07 08:35:23 字数 2343 浏览 0 评论 0原文

json ="{
   "data": [
      {
         "id": "1000",
         "from": {
            "name": "Anthony Waema",
            "category": "message",
            "id": "192"
         },
         "message": "this is the message",
         "updated_time": "2001-05-06T19:34:15+0000",
         "likes": {
            "data": [
               {
                  "id": "100001692250255",
                  "name": "\u00dcnal Turanl\u0131"
               },
               {
                  "id": "100001060078996",
                  "name": "S\u00e9f\u00e2 K\u00e2ql\u00e4Nn"
               }]
              },
         {
         "id": "10150186255753553",
         "from": {
            "name": "another name",
            "category": "message",
            "id": "100001"
         },
         "message": "this is the message",
         "updated_time": "2001-04-06T19:34:15+0000",
         "likes": {
            "data": [
               {
                  "id": "1002345",
                  "name": "\u00dcnal Turanl\u0131"
               },
               {
                  "id": "100234",
                  "name": "S\u00e9f\u00e2 K\u00e2ql\u00e4Nn"
               }]
              }
         }
]
}";

public class Allstatus
    {
        public List<sdata> data { get; set; }
        public scpaging paging { get; set; }
    }

public class sdata
    {

        public string id { get; set; }
        public sfrom from { get; set; }
        public string message { get; set; }
        public string updated_time {get; set;}
        public List<likesdata> likes { get; set; }
    }
   public class likesdata
    {
        public string id{ get; set; }
        public string name{ get; set; }
    }
public class sfrom 
{
      public string name {get; set;}
      public string category {get; set;}
      public string id {get; set;}
}

                JavaScriptSerializer ser = new JavaScriptSerializer();
                page = ser.Deserialize<allstatus>(json);
                foreach (sdata cd in page.data)
                {
                    foreach(likesdata ld in  cd.likes.data)
                    {
                        Console.WriteLine(ld.id+"\t"+ld.name);
                    }

                }

问题: 我需要解析 json 并检索喜欢的数据。 我可以访问“来自”数据,但不能访问“喜欢”数据。执行此操作时,我收到空引用错误。这里需要帮助..谢谢。

json ="{
   "data": [
      {
         "id": "1000",
         "from": {
            "name": "Anthony Waema",
            "category": "message",
            "id": "192"
         },
         "message": "this is the message",
         "updated_time": "2001-05-06T19:34:15+0000",
         "likes": {
            "data": [
               {
                  "id": "100001692250255",
                  "name": "\u00dcnal Turanl\u0131"
               },
               {
                  "id": "100001060078996",
                  "name": "S\u00e9f\u00e2 K\u00e2ql\u00e4Nn"
               }]
              },
         {
         "id": "10150186255753553",
         "from": {
            "name": "another name",
            "category": "message",
            "id": "100001"
         },
         "message": "this is the message",
         "updated_time": "2001-04-06T19:34:15+0000",
         "likes": {
            "data": [
               {
                  "id": "1002345",
                  "name": "\u00dcnal Turanl\u0131"
               },
               {
                  "id": "100234",
                  "name": "S\u00e9f\u00e2 K\u00e2ql\u00e4Nn"
               }]
              }
         }
]
}";

public class Allstatus
    {
        public List<sdata> data { get; set; }
        public scpaging paging { get; set; }
    }

public class sdata
    {

        public string id { get; set; }
        public sfrom from { get; set; }
        public string message { get; set; }
        public string updated_time {get; set;}
        public List<likesdata> likes { get; set; }
    }
   public class likesdata
    {
        public string id{ get; set; }
        public string name{ get; set; }
    }
public class sfrom 
{
      public string name {get; set;}
      public string category {get; set;}
      public string id {get; set;}
}

                JavaScriptSerializer ser = new JavaScriptSerializer();
                page = ser.Deserialize<allstatus>(json);
                foreach (sdata cd in page.data)
                {
                    foreach(likesdata ld in  cd.likes.data)
                    {
                        Console.WriteLine(ld.id+"\t"+ld.name);
                    }

                }

problem:
I need to parse the json and retrieve likes data.
I can access "from" data but not "likes" data.. I get nullreference error when I do this. help needed here.. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

隱形的亼 2024-11-14 08:35:23

编辑2:

参考https://gist.github.com/973510,其从返回的 json 中可以清楚地看出,如果特定的 Facebook 消息没有任何“喜欢”,则返回的 json 不包含名为 likes 的属性。因此,sdata 对象likes 属性为 null。这就是服务器返回数据的方式。

有两种方法可以解决这个问题。要么手动检查 likes 是否为空。或者在 sdata 构造函数中初始化 likes 属性。并在 likesdatacollection 构造函数中初始化 Likesdata 列表。

代码:

public class sdata
{
    // other properties

    public likedatacollection likes { get; set; }

    public sdata()
    {
        likes = new likedatacollection();
    }
}

public class likedatacollection
{
    public List<likesdata> data { get; set; }

    public likedatacollection()
    {
        data = new List<likesdata>();
    }
}

这样,即使 fb 没有返回任何 like,构造函数也会初始化属性,因此它们不会为 null。然后您可以检查是否 likes.data.Count > 0 。如果是,则fb返回点赞,否则fb不返回点赞。

编辑1:

从OP的评论中,很明显json格式正确。意思是,json 是从某个服务器 api 检索到的。因此,sdata 类才是罪魁祸首。请查看此要点以获取完整的解决方案。

简短的版本。对于最简单的情况,您的 C# 类需要遵循与 json 完全相同的结构。根据 json,data 有一个名为 likes 的属性。 likes 对象有一个名为 data 的属性,它是具有属性 id 和 name 的对象数组。

因此,您的 C# 类 sdata 应该有一个名为 likes 且类型为 likesdatacollection 的属性。这个类应该有一个 List 类型的属性 data ...

偏离主题,人们通常似乎更喜欢 Json.Net ...所以你可能想使用那。我使用它的原因是因为我需要它在 .Net 2.0 代码库中工作......

Edit2:

Referring https://gist.github.com/973510, its clear from the returned json, that if a particular facebook message doesnt have any likes, then the returned json doesnt contain a property called likes. Hence likes property of sdata object is null. Thats just how the server returns the data.

There are two ways you can deal with this. Either do a manual check whether likes is null. Or initialize the likes property in the sdata constructor. And initialize the likesdata list in the likesdatacollection constructor.

Code:

public class sdata
{
    // other properties

    public likedatacollection likes { get; set; }

    public sdata()
    {
        likes = new likedatacollection();
    }
}

public class likedatacollection
{
    public List<likesdata> data { get; set; }

    public likedatacollection()
    {
        data = new List<likesdata>();
    }
}

This way, even if fb doesnt return any likes, the constructors will initialize the properties, so they will not be null. You can then check whether likes.data.Count > 0. If yes, then fb returned likes, else fb didnt return likes.

Edit1:

From the OP's comment, its clear that the json is properly formed. Meaning, the json is as retrieved from some server api. Therefore it is the sdata class that is the culprit. Please look at this gist for the full solution.

The short version. For the simplest case, your c# classes need to follow the exact same structure as your json. As per the json, data has a property called likes. the likes object has a property called data which is an array of objects with properties id and name.

So your c# class sdata should have a property called likes of type likesdatacollection. This class should have a property data of type List<likesdata>...

Off topic, people generally seem to prefer Json.Net ... so you may want to use that. The reason I use it is because I need it to work in a .Net 2.0 code base ...

辞别 2024-11-14 08:35:23

您应该尝试通过 JSON Lint 等验证器运行 JSON。这应该可以帮助您找到任何 JSON 错误。

You should try running your JSON through a validator like JSON Lint. That should help you find any JSON errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文