我创建了一个用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#的新手,所以我有些错误,请帮我修好课程。
答案 0 :(得分: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/,看看你自己......
答案 1 :(得分: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; }
}
答案 2 :(得分:0)
许多人不知道,但Visual Studio自己做了。 复制JSON(Ctrl + c),打开.cs文件,转到编辑 - &gt;选择性粘贴 - &gt;粘贴JSON类。 PS:VS2012 + .net3.5 +