【发布时间】:2017-09-03 04:46:41
【问题描述】:
我需要从 JSON 字符串中提取值,以便比较它们。 我只需要验证它们是否有序(升序/降序)。 我打算检查第一个和第二个“选择”并进行比较。 我没有更高级的东西。
编辑/更新: 我如何在这种类型的查询中使用通配符 (*) 来跳过每个段?
string one = (string)o[this.Context[*WILDCARD*]["cid1"]].ToString();
/* this works, but has too many []
string one = (string)o[this.Context["partner"]]
[this.Context["campaign"]]
[this.Context["segment1"]]
[this.Context["segment2"]]
[this.Context["qid2"]]
["community"]
[this.Context["cid1"]].ToString();
*/
{
"partner": {
"campaign": {
"round1": {
"round2": {
"def123": {
"community": {
"choicec": 28
},
"user": {
"choice": "choicec",
"writeDateUTC": "2015-06-15T17:21:59Z"
}
}
},
"abc321": {
"community": {
"choicec": 33
},
"user": {
"choice": "choicec",
"writeDateUTC": "2015-06-15T17:21:59Z"
}
}
}
}
}
}
【问题讨论】:
-
听起来是个很酷的项目!似乎是什么问题?
-
我尝试过使用 LINQ、Jtoken、Jpath 等,但都失败或返回空。所以我接触到社区
-
用 JSON.NET 试试这个(添加 Nuget 包后需要
using Newtonsoft.Json):dynamic d = JsonSerializer.Deserialize(yourJsonString);然后使用d.partner.campaign.round1.def123.community.choicec访问选项编号。 -
为什么是
dynamic?一定有一个具体的课程。 -
您的 JSON 中没有
"cid1"属性。只是为了确认一下,您想比较"choicec": 28和"choicec": 33属性的值以检查它们是否正常?