【发布时间】:2018-02-14 13:17:05
【问题描述】:
我无法将数据序列化为 C# 对象,因为同一组对象的 JSON 结构不同。是否有某种函数可以让您搜索所有 JSON 属性及其子属性以返回您搜索的属性的值?
我通过 API 接收的数据示例(我正在尝试返回属性“propertyIamLookingFor”的值):
第一个例子
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"propertyIamLookingFor": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
}
}}
第二个例子
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"propertyIamLookingFor": 32
}}
第三个例子
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"height": 500,
"propertyIamLookingFor": 11
}
}
}}
请注意,无论其位置如何,该属性始终具有相同的名称,因此我知道该属性的名称并尝试返回该值。
*如果有人想知道,这是我正在访问的一个糟糕的 API,但没有选择不使用它的选项。
【问题讨论】:
-
你看过 JsonPath 吗? newtonsoft.com/json/help/html/QueryJsonSelectTokenJsonPath.htm
-
该函数假定您熟悉该值,但我并不熟悉,它实际上可以是任何东西,因为它们是我正在尝试阅读的消息。我正在寻找一个函数,您可以在其中输入属性名称并取回该属性的值,但无论 JSON 结构中的属性位置如何,因为它的位置是完全可变的。
-
如果没有你收到的数据类型的例子,我会在黑暗中射击试图在这里回答你。
-
@DanielleSummers 这里是例子
-
那么找到该属性的规则是什么?姓名?你可以enumerate json找到它。
标签: c# json properties json.net