gpt4 book ai didi

c# - 解析没有反射对象的复杂 JSON?

转载 作者:太空宇宙 更新时间:2023-11-03 22:02:10 25 4
gpt4 key购买 nike

我使用的 API 输出具有许多属性(包括子对象)的 JSON 对象。到目前为止,我在 C# 中找到的所有 JSON 示例都假设 .Net 对象已序列化为 JSON,并且可以反序列化回 .Net 对象。

我正在尝试找到一个解决方案,我可以将 JSON 读取到一些通用对象中,然后我可以通过从通用对象中选择值来手动为我的业务对象属性分配值。

可能类似于 ADO.Nets Datables 和 DataRows,您可以在其中将列名传递给 DataRow 对象并获取列的值。这可能有点太高级了,但基本上我希望能够通过遍历 JSON 对象来读取它们,并通过传递属性名称从对象中选择值。这意味着我不会将所有属性反序列化到我的业务对象中,我只会从 JSON 对象中获取特定属性并将它们的值分配给我的业务对象的属性。

执行此操作的好的解决方案是什么?

这是从 API 输出的两个对象的示例:

{
"appt": [
{
"d": 1331717080000,
"id": "112115",
"inv": [
{
"comp": [
{
"alarm": [
{
"action": "DISPLAY",
"desc": [
{}
],
"trigger": [
{
"rel": [
{
"m": 5,
"neg": true,
"related": "START"
}
]
}
]
}
],
"apptId": "112115",
"calItemId": "112115",
"ciFolder": "112111",
"class": "PUB",
"compNum": 0,
"d": 1331717080000,
"desc": [
{
"_content": ""
}
],
"descHtml": [
{
"_content": "<html><body></body></html>"
}
],
"e": [
{
"d": "20120314T130000",
"tz": "Pacific/Auckland",
"u": 1331683200000
}
],
"fb": "B",
"fba": "B",
"isOrg": true,
"loc": "",
"method": "PUBLISH",
"name": "Test Appt",
"noBlob": true,
"or": {
"a": "liam@url.com",
"d": "Liam Smith",
"url": "liam@url.com"
},
"rsvp": false,
"s": [
{
"d": "20120314T090000",
"tz": "Pacific/Auckland",
"u": 1331668800000
}
],
"seq": 0,
"status": "CONF",
"transp": "O",
"uid": "aa2e4fe4-c636-4660-ae92-3e7041fe5669",
"url": "",
"x_uid": "aa2e4fe4-c636-4660-ae92-3e7041fe5669"
}
],
"compNum": 0,
"id": 112114,
"seq": 0,
"type": "appt",
"tz": [
{
"daylight": [
{
"hour": 2,
"min": 0,
"mon": 9,
"sec": 0,
"week": -1,
"wkday": 1
}
],
"dayname": "NZDT",
"dayoff": 780,
"id": "Pacific/Auckland",
"standard": [
{
"hour": 3,
"min": 0,
"mon": 4,
"sec": 0,
"week": 1,
"wkday": 1
}
],
"stdname": "NZST",
"stdoff": 720
}
]
}
],
"l": "112111",
"rev": 72672,
"s": 0,
"uid": "aa2e4fe4-c636-4660-ae92-3e7041fe5669"
},
{
"d": 1331717176000,
"id": "112117",
"inv": [
{
"comp": [
{
"alarm": [
{
"action": "DISPLAY",
"desc": [
{}
],
"trigger": [
{
"rel": [
{
"m": 5,
"neg": true,
"related": "START"
}
]
}
]
}
],
"apptId": "112117",
"calItemId": "112117",
"ciFolder": "112111",
"class": "PUB",
"compNum": 0,
"d": 1331717176000,
"desc": [
{
"_content": ""
}
],
"descHtml": [
{
"_content": "<html><body></body></html>"
}
],
"e": [
{
"d": "20120315T120000",
"tz": "Pacific/Auckland",
"u": 1331766000000
}
],
"fb": "B",
"fba": "B",
"isOrg": true,
"loc": "",
"method": "PUBLISH",
"name": "test 2",
"noBlob": true,
"or": {
"a": "liam@url.com",
"d": "Liam Smith",
"url": "liam@url.com"
},
"rsvp": false,
"s": [
{
"d": "20120315T100000",
"tz": "Pacific/Auckland",
"u": 1331758800000
}
],
"seq": 0,
"status": "CONF",
"transp": "O",
"uid": "4081f711-9742-42bd-a5dc-8bc9ddc3305d",
"url": "",
"x_uid": "4081f711-9742-42bd-a5dc-8bc9ddc3305d"
}
],
"compNum": 0,
"id": 112116,
"seq": 0,
"type": "appt",
"tz": [
{
"daylight": [
{
"hour": 2,
"min": 0,
"mon": 9,
"sec": 0,
"week": -1,
"wkday": 1
}
],
"dayname": "NZDT",
"dayoff": 780,
"id": "Pacific/Auckland",
"standard": [
{
"hour": 3,
"min": 0,
"mon": 4,
"sec": 0,
"week": 1,
"wkday": 1
}
],
"stdname": "NZST",
"stdoff": 720
}
]
}
],
"l": "112111",
"nextAlarm": 1331758500000,
"rev": 72674,
"s": 0,
"uid": "4081f711-9742-42bd-a5dc-8bc9ddc3305d"
}
]
}

最佳答案

如果您使用的是 Json.Net您可以使用 dynamic 关键字。这是使用您的 json 字符串的示例代码。

dynamic obj = JsonConvert.DeserializeObject(jsonstr);
Console.WriteLine("{0} {1}", obj.appt[0].inv[0].comp[0].method, obj.appt[1].id);

foreach (var appt in obj.appt)
{
Console.WriteLine("{0} {1}", appt.inv[0].comp[0].method, appt.id);
}

如果您使用的是 < 4.0 的 framerork,也可以使用更丑陋的用法

JObject obj = (JObject)JsonConvert.DeserializeObject(jsonstr);
foreach (var appt in obj["appt"])
{
Console.WriteLine("{0} {1}", appt["inv"][0]["comp"][0]["method"], appt["id"]);
}

关于c# - 解析没有反射对象的复杂 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9700293/

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