创建从json到C#的类解析 [英] Create class parse from json to c#

查看:247
本文介绍了创建从json到C#的类解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个类,可以使用json.net库将json解析为C#,但是可能是我的类出了问题.

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

我的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#的新手,所以我有一些错误,请帮助我修复班级.

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

推荐答案

您的课程应该是:

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/并看到自己...

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

这篇关于创建从json到C#的类解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆