我有一个 JArrays 的 JArray,但我想将它展平成一个 JObjects 的 JArray。我已经实现了一个 foreach 循环,它遍历 JArray 中的每个 JArray。我需要知道如何将每个子 JArray 展平为 JObject。下面是一个例子:[ { "item": [ { "fieldName": "Name", "value": "Andy" }, { "fieldName": "Phone", "value": "678-905-9872" } ] }, { "item": [ { "fieldName": "Name", "value": "John" }, { "fieldName": "Phone", "value": "688-954-5678" } ] }, { "item": [ { "fieldName": "Name", "value": "Ashley" }, { "fieldName": "Phone", "value": "+44 671 542 8945" } ] }, { "item": [ { "fieldName": "Name", "value": "Avi" }, { "fieldName": "Phone", "value": "(212)-908-7772" } ] } ]我希望每个项目都是单个 JObject,从而产生以下 JArray:[ { "Name": "Andy" "Phone": "678-905-9872" }, { "Name": "John" "Phone": "688-954-5678" { "Name": "Ashley" "Phone": "+44 671 542 8945" }, { "Name": "Avi" "Phone": "(212)-908-7772" } ]谢谢!
1 回答
www说
TA贡献1775条经验 获得超8个赞
var jArr = new JArray(JArray.Parse(JSON)
.Select(x => new JObject(new JProperty("Name", x["item"][0]["Name"]),
new JProperty("Phone", x["item"][1]["Phone"])
)
)
);
var str = JsonConvert.SerializeObject(jArr, Formatting.Indented);
str 将是:
[
{
"Name": "Andy",
"Phone": "(785) 241-6200"
},
{
"Name": "Arthur Song",
"Phone": "(212) 842-5500"
},
{
"Name": "Ashley James",
"Phone": "+44 191 4956203"
},
{
"Name": "Avi Green",
"Phone": "(212) 842-5500"
}
]
- 1 回答
- 0 关注
- 226 浏览
添加回答
举报
0/150
提交
取消