我有一些复杂的JSON,看起来像这样:
{
"memberdetails": [
{
"id": 0,
"label": "General Details",
"visible": true,
"properties": [
{
"label": "Address",
"description": "Residential or Postal Address",
"view": "textarea",
"config": {},
"hideLabel": false,
"validation": {
"mandatory": false,
"pattern": null
},
"id": 0,
"value": "test 1",
"alias": "address",
"editor": "Umbraco.TextboxMultiple",
"visible": "true"
},
{
"label": "State",
"description": "State of residence",
"view": "textbox",
"config": {},
"hideLabel": false,
"validation": {
"mandatory": false,
"pattern": null
},
"id": 0,
"value": "test 2",
"alias": "state",
"editor": "Umbraco.Textbox",
"visible": "true"
}
]
},
{
"id": 1,
"label": "Other Details",
"visible": true,
"properties": [
{
"label": "Address",
"description": "Residential or Postal Address",
"view": "textarea",
"config": {},
"hideLabel": false,
"validation": {
"mandatory": false,
"pattern": null
},
"id": 0,
"value": "test_otherdetails1 ",
"alias": "aliastest",
"editor": "Umbraco.TextboxMultiple",
"visible": "true"
},
{
"label": "State",
"description": "State of residence",
"view": "textbox",
"config": {},
"hideLabel": false,
"validation": {
"mandatory": false,
"pattern": null
},
"id": 0,
"value": "test_otherdetails2",
"alias": "aliastest2",
"editor": "Umbraco.Textbox",
"visible": "true"
}
]
},
{
"id": 2,
"label": " Details",
"visible": true,
"properties": [
{
"label": "Address",
"description": "Residential or Postal Address",
"view": "textarea",
"config": {},
"hideLabel": false,
"validation": {
"mandatory": false,
"pattern": null
},
"id": 0,
"value": " Details1",
"alias": "Details1Test",
"editor": "Umbraco.TextboxMultiple",
"visible": "true"
},
{
"label": "State",
"description": "State of residence",
"view": "textbox",
"config": {},
"hideLabel": false,
"validation": {
"mandatory": false,
"pattern": null
},
"id": 0,
"value": "Details2",
"alias": "Details12est",
"editor": "Umbraco.Textbox",
"visible": "true"
}
]
}
]
}
我正在尝试从JSON中提取alias
和value
项,并将它们放入单个JObject中,如下所示:
{
"address": "test 1",
"state": "test 2",
"aliastest": "test_otherdetails1 ",
"aliastest2": "test_otherdetails2",
"Details1Test": " Details1",
"Details12est": "Details2"
}
使用Json.Net有一种简单的方法吗?请注意,alias
值在实际JSON中都是不同的。
答案 0 :(得分:0)
是的,您可以像这样转换您的JSON:
JObject obj = JObject.Parse(json);
JObject result = new JObject(
obj["memberdetails"]
.SelectMany(jt => jt["properties"])
.Select(jt => new JProperty((string)jt["alias"],jt["value"]))
);
Console.WriteLine(result.ToString());