使用包含对象中的各个属性转换 JSON 数组中的名称-值对数组
Transforming an array of name-value pairs in a JSON array with individual properties in the containing object
我正在尝试将 JSON 数组转换为对象格式。
示例:
{
"country": "USA",
"date": "2019-6-30",
"Speaker": [
{
"id": "name",
"value": "Tiger"
},
{
"id": "age",
"value": "35"
},
{
"id": "topic",
"value": ".NET"
}
]
}
我想将其转换为:
{
"country": "USA",
"date": "2019-6-30",
"name": "Tiger",
"age": 35,
"topic": ".NET"
}
我尝试了几种方法,但都没有成功。看来我无法获取内部数组的值。请帮忙。
你只需要几个 类 来反序列化这个 JSON,例如:
public class Data
{
public string Country { get; set; }
public string Date { get; set; }
// Deserialise the array as a list of 'SpeakerItem'
public List<SpeakerItem> Speaker { get; set; }
// These will throw exceptions if the id doesn't match, but it's a start
public string Name => Speaker.Single(s => s.Id == "name").Value;
public string Age => Speaker.Single(s => s.Id == "age").Value;
public string Topic => Speaker.Single(s => s.Id == "topic").Value;
}
public class SpeakerItem
{
public string Id { get; set; }
public string Value { get; set; }
}
现在你可以这样做:
var value = JsonConvert.DeserializeObject<Data>(json);
我使用 JSON.Net 有类似的东西,首先你的 json 是错误的(你在国家/地区行的末尾有点)。我用过 DynamoObjects。
string json = @"
{
""country"": ""USA"",
""date"": ""2019-6-30"",
""Speaker"" : [
{
""id"": ""name"",
""value"": ""Tiger""
},
{
""id"": ""age"",
""value"": ""35""
},
{
""id"": ""topic"",
""value"": "".NET""
},
]
}";
dynamic animalJson = JsonConvert.DeserializeObject<dynamic>(json);
dynamic animal = new ExpandoObject();
animal.country = animalJson.country;
animal.date = animalJson.date;
animal.name = animalJson.Speaker[0].value;
animal.age = animalJson.Speaker[1].value;
animal.topic = animalJson.Speaker[2].value;
string modifiedAnimalJson = JsonConvert.SerializeObject(animal);
您可以使用 Json.Net 的 LINQ-to-JSON API (JObjects) 来转换您的 JSON:
JObject root = JObject.Parse(json);
JProperty arrayProp = root.Properties()
.Where(jp => jp.Value.Type == JTokenType.Array)
.FirstOrDefault();
if (arrayProp != null)
{
foreach (JObject item in arrayProp.Value.Children<JObject>())
{
root[(string)item["id"]] = item["value"];
}
arrayProp.Remove();
}
json = root.ToString();
此解决方案不依赖于具有任何特定名称的数组 属性,也不关心项目 ID 是什么。但是,如果数组中有任何 id 与根对象中现有的 属性 重叠,则数组中的值将替换根对象中已有的值。同样,如果数组中有任何重复的 id,最后一个将 "win".
我正在尝试将 JSON 数组转换为对象格式。
示例:
{
"country": "USA",
"date": "2019-6-30",
"Speaker": [
{
"id": "name",
"value": "Tiger"
},
{
"id": "age",
"value": "35"
},
{
"id": "topic",
"value": ".NET"
}
]
}
我想将其转换为:
{
"country": "USA",
"date": "2019-6-30",
"name": "Tiger",
"age": 35,
"topic": ".NET"
}
我尝试了几种方法,但都没有成功。看来我无法获取内部数组的值。请帮忙。
你只需要几个 类 来反序列化这个 JSON,例如:
public class Data
{
public string Country { get; set; }
public string Date { get; set; }
// Deserialise the array as a list of 'SpeakerItem'
public List<SpeakerItem> Speaker { get; set; }
// These will throw exceptions if the id doesn't match, but it's a start
public string Name => Speaker.Single(s => s.Id == "name").Value;
public string Age => Speaker.Single(s => s.Id == "age").Value;
public string Topic => Speaker.Single(s => s.Id == "topic").Value;
}
public class SpeakerItem
{
public string Id { get; set; }
public string Value { get; set; }
}
现在你可以这样做:
var value = JsonConvert.DeserializeObject<Data>(json);
我使用 JSON.Net 有类似的东西,首先你的 json 是错误的(你在国家/地区行的末尾有点)。我用过 DynamoObjects。
string json = @"
{
""country"": ""USA"",
""date"": ""2019-6-30"",
""Speaker"" : [
{
""id"": ""name"",
""value"": ""Tiger""
},
{
""id"": ""age"",
""value"": ""35""
},
{
""id"": ""topic"",
""value"": "".NET""
},
]
}";
dynamic animalJson = JsonConvert.DeserializeObject<dynamic>(json);
dynamic animal = new ExpandoObject();
animal.country = animalJson.country;
animal.date = animalJson.date;
animal.name = animalJson.Speaker[0].value;
animal.age = animalJson.Speaker[1].value;
animal.topic = animalJson.Speaker[2].value;
string modifiedAnimalJson = JsonConvert.SerializeObject(animal);
您可以使用 Json.Net 的 LINQ-to-JSON API (JObjects) 来转换您的 JSON:
JObject root = JObject.Parse(json);
JProperty arrayProp = root.Properties()
.Where(jp => jp.Value.Type == JTokenType.Array)
.FirstOrDefault();
if (arrayProp != null)
{
foreach (JObject item in arrayProp.Value.Children<JObject>())
{
root[(string)item["id"]] = item["value"];
}
arrayProp.Remove();
}
json = root.ToString();
此解决方案不依赖于具有任何特定名称的数组 属性,也不关心项目 ID 是什么。但是,如果数组中有任何 id 与根对象中现有的 属性 重叠,则数组中的值将替换根对象中已有的值。同样,如果数组中有任何重复的 id,最后一个将 "win".