gpt4 book ai didi

c# - 创建从 json 到 c# 的类解析

转载 作者:太空宇宙 更新时间:2023-11-03 21:43:26 24 4
gpt4 key购买 nike

我已经创建了一个类来使用 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# 的新手,所以我有一些错误,请帮我修复我的类(class)。

最佳答案

你的类(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/看看你自己……

关于c# - 创建从 json 到 c# 的类解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18248925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com