1

我已经创建了一个类来使用 json.net 库从 json 解析到 C#,但可能是我的类有问题。

我的 Json 代码:

{
   "data": [
      {
         "category": "Health/beauty",
         "category_list": [
            {
               "id": "144873802247220",
               "name": "Cosmetics & Beauty Supply"
            }
         ],
         "name": "T2Perfume",
         "created_time": "2013-06-19T12:22:46+0000",
         "id": "493082927378290"
      },
      {
         "category": "Company",
         "category_list": [
            {
               "id": "2200",
               "name": "Company"
            }
         ],
         "name": "H\u1ed9i th\u1ea3o Vi\u1ec7t - Hoithao.vn",
         "created_time": "2013-06-19T04:51:18+0000",
         "id": "195702227137898"
      },
      {
         "category": "Library",
         "name": "Mang Y\u00eau Th\u01b0\u01a1ng X\u1ebfp V\u00e0o Qu\u00e1 Kh\u1ee9 :\">",
         "created_time": "2013-06-19T04:32:57+0000",
         "id": "351541048264584"
      },
      {
         "category": "Artist",
         "name": "H\u1ed9i nh\u1eefng ng\u01b0\u1eddi ph\u00e1t cu\u1ed3ng v\u00ec s\u1ef1 \u0111\u1eb9p trai c\u1ee7a M-TP",
         "created_time": "2013-06-19T04:32:25+0000",
         "id": "243741269085846"
      },
      {
         "category": "Club",
         "name": "Thay l\u1eddi mu\u1ed1n n\u00f3i",
         "created_time": "2013-06-19T04:32:21+0000",
         "id": "10150153637275305"
      },
      {
         "category": "Community organization",
         "name": "V\u1edbi Em, Anh v\u1eabn lu\u00f4n v\u00e0 m\u00e3i lu\u00f4n l\u00e0 \u0111i\u1ec1u Em mu\u1ed1n gi\u1eef b\u00ean m\u00ecnh :\")",
         "created_time": "2013-06-19T04:32:12+0000",
         "id": "420671217951259"
      },
      {
         "category": "Community",
         "name": "C\u00f3 th\u1ec3 b\u1ea1n ch\u01b0a bi\u1ebft?",
         "created_time": "2013-06-19T04:32:09+0000",
         "id": "101397210017337"
      },
      {
         "category": "Artist",
         "name": "MC Tr\u1ea5n Th\u00e0nh",
         "created_time": "2013-06-19T04:32:05+0000",
         "id": "488035214559138"
      },
      {
         "category": "Tv show",
         "name": "2! Idol",
         "created_time": "2013-06-19T04:32:02+0000",
         "id": "124501338300"
      },
      {
         "category": "Tv show",
         "name": "Family Outing",
         "created_time": "2013-06-19T04:31:54+0000",
         "id": "108518482506735"
      },
      {
         "category": "Tv show",
         "name": "Running Man",
         "created_time": "2013-06-19T04:31:49+0000",
         "id": "147481878610796"
      },
      {
         "category": "Community",
         "name": "Shop Gi\u00e0y , T\u00fai x\u00e1ch, ba l\u00f4 Cao C\u1ea5p",
         "created_time": "2012-12-09T02:29:25+0000",
         "id": "507495189262653"
      }
   ],
   "paging": {
      "next": "https://graph.facebook.com/100000479404497/likes?access_token=CAAAAAITEghMBAMYFbPO0KowgvkCclrZB0ZAYa7v9ssyyfQXaPfyZB9C1zwt6OuZCzmlbits5nb3MKVbUc7d9OYTYyNv2ZAEckAKmUdZAgUXbXRVDLZAE1aUU2QEZAp5aGtT4qbhKbXy6Yu7HgDxkwZCleu0JbvWQAKdkZD&limit=5000&offset=5000&__after_id=507495189262653"
   }
}

这是我建立的课程:

public class abc{
         public data2[] data { get; set; }
         public paging2 paging { get; set; }

         public struct data2 {
             public string category { get; set; }
             public Array category_list { get; set; }
             public string name { get; set; }
             public string created_time { get; set; }
             public string id { get; set; }
         }
         public struct paging2 {
             public string next { get; set; }
         }
     }

我是 json 和 C# 的新手,所以我有一些错误,请帮我修复我的课程。

4

3 回答 3

4

你的课程应该是:

public class CategoryList
{
    public string id { get; set; }
    public string name { get; set; }
}

public class Datum
{
    public string category { get; set; }
    public List<CategoryList> category_list { get; set; }
    public string name { get; set; }
    public string created_time { get; set; }
    public string id { get; set; }
}

public class Paging
{
    public string next { get; set; }
}

public class RootObject
{
    public List<Datum> data { get; set; }
    public Paging paging { get; set; }
}

var obj = JsonConvert.DeserializeObject<RootObject>(json);

把你的 json 放到http://json2csharp.com/看看你自己...

于 2013-08-15T08:33:03.953 回答
0

试试这个结构:

public class Paging
{
    public string Next { get; set; }
}

public class CategoryItem
{
    public string Name { get; set; }
    public string Id { get; set; }
}

public class Data
{
    public string Category { get; set; }
    public string Name { get; set; }
    public string Created_time { get; set; }
    public string Id { get; set; }
    public CategoryItem[] Category_list { get; set; }
}

public class Abc
{
    public Data[] Data { get; set; }
    public Paging Paging { get; set; }
}
于 2013-08-15T08:34:12.633 回答
0

很多人不知道,Visual Studio 自己做的。复制 JSON (Ctrl + c),打开 .cs 文件,转到编辑 -> 选择性粘贴 -> 粘贴 JSON 类。PS:VS2012+.net3.5+

于 2015-05-28T19:56:44.827 回答