Create class parse from json to c#

回眸只為那壹抹淺笑 提交于 2020-01-06 02:24:14

问题


I've created a class to parse from json to C# with json.net library, but may be my class has something wrong.

My Json code:

{
   "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"
   }
}

Here is my class I've build:

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

I'm newbie in json and C# so I've some mistakes, please help me fix my class.


回答1:


Your classes should be:

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

Put your json to http://json2csharp.com/ and see yourself...




回答2:


Try this structure instead:

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



回答3:


Many people do not know, but Visual Studio does it himself. Copy the JSON (Ctrl + c), with the open .cs file, go to edit -> Paste Special -> Paste JSON Classes. PS: VS2012 + .net3.5 +



来源:https://stackoverflow.com/questions/18248925/create-class-parse-from-json-to-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!