Newtonsoft JSON - 不同的JSON结构,对象VS阵列
我有这样的JSON结构:Newtonsoft JSON - 不同的JSON结构,对象VS阵列
[ {
"key1": "value",
"key2": "value",
"key3": "TYPE_ONE",
"extraData": {
"key4": "value",
"key5": "value",
"key6": {
"key7": {
"key8": "value",
"key9": "value",
"key10": "value"
}
}
}
},
{
"key1": "value",
"key2": "value",
"key3": "TYPE_TWO",
"extraData": {
"key4": "value",
"key5": "value",
"key6": {
"key7": [
{
"key8": "value",
"key9": "value",
"key10": "value"
},
{
"key8": "value1",
"key9": "value1",
"key10": "value1"
},
{
"key8": "value2",
"key9": "value2",
"key10": "value2"
}
]
}
}
}
]
我想在C#
与using Newtonsoft.Json;
到反序列化。 JSON有两种结构。
1. CASE - >当“key3”:“TYPE_ONE”时,则key7是对象。
2. CASE - >当“key3”:“TYPE_TWO”时,则key7为数组。
如果在课堂上
public class key6 {
public key7 key7Item { get; set; }
}
key7Item设置为对象的工作只有当我得到的所有对象从反应是 “KEY3”: “TYPE_ONE”
如果我设置key7Item为列表然后才工作,如果来自响应的所有项目是“key3”:“TYPE_TWO”。
我可以配置Newtonsoft分析器来解析数组,如果它有数组或对象,如果它有对象?
回答:
简短的回答 - 如果你可以避免它,不要这样做。通过为阵列提供不同的密钥名称,可以节省大量复杂性。
很长的答案,你可以做到,但它很痛苦。如果有人知道如何做,而无需重新序列化所生成的J-对象(即保持旁边的反序列化对象的JSON),我很想知道:
private static readonly string Json = "[\r\n {\r\n \"key1\": \"value\",\r\n \"key2\": \"value\",\r\n \"key3\": \"TYPE_ONE\",\r\n \"extraData\": {\r\n \"key4\": \"value\",\r\n \"key5\": \"value\",\r\n \"key6\": {\r\n \"key7\": {\r\n \"key8\": \"value\",\r\n \"key9\": \"value\",\r\n \"key10\": \"value\"\r\n }\r\n }\r\n }\r\n },\r\n {\r\n \"key1\": \"value\",\r\n \"key2\": \"value\",\r\n \"key3\": \"TYPE_TWO\",\r\n \"extraData\": {\r\n \"key4\": \"value\",\r\n \"key5\": \"value\",\r\n \"key6\": {\r\n \"key7\": [\r\n {\r\n \"key8\": \"value\",\r\n \"key9\": \"value\",\r\n \"key10\": \"value\"\r\n },\r\n {\r\n \"key8\": \"value1\",\r\n \"key9\": \"value1\",\r\n \"key10\": \"value1\"\r\n },\r\n {\r\n \"key8\": \"value2\",\r\n \"key9\": \"value2\",\r\n \"key10\": \"value2\"\r\n }\r\n ]\r\n }\r\n }\r\n }\r\n]"; private static void Deserialize()
{
var switchers = JsonConvert.DeserializeObject<JObject[]>(Json);
var deserializedType1List = new List<TargetClass<Key6Type1>>();
var deserializedType2List = new List<TargetClass<Key6Type2>>();
foreach (var switcher in switchers)
{
if (switcher["key3"].Value<string>() == "TYPE_ONE")
{
deserializedType1List.Add(JsonConvert.DeserializeObject<TargetClass<Key6Type1>>(JsonConvert.SerializeObject(switcher)));
}
else
{
deserializedType2List.Add(JsonConvert.DeserializeObject<TargetClass<Key6Type2>>(JsonConvert.SerializeObject(switcher)));
}
}
//Do something with the objects...
}
private class TargetClass<TKey6Type>
{
public string key1 { get; set; }
public string key2 { get; set; }
public string key3 { get; set; }
public ExtraData<TKey6Type> extraData { get; set; }
}
private class ExtraData<TKey6Type>
{
public string key4 { get; set; }
public string key5 { get; set; }
public TKey6Type key6 { get; set; }
}
private class Key6Type2
{
public Key7[] key7 { get; set; }
}
private class Key6Type1
{
public Key7 key7 { get; set; }
}
private class Key7
{
public string key8 { get; set; }
public string key9 { get; set; }
public string key10 { get; set; }
}
以上是 Newtonsoft JSON - 不同的JSON结构,对象VS阵列 的全部内容, 来源链接: utcz.com/qa/263233.html