Newtonsoft JSON-不同的JSON结构,对象与数组



我有这个JSON结构:

[
  {
    "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。案例 ->当" key3":" type_one"时,key7是对象。
2。案例 ->当" key3":" type_two"时,key7是数组。
如果在类中

public class key6
{
    public key7 key7Item { get; set; }
}

key7Item 仅在我从响应中获取所有对象时,才能工作" key3":" type_one"
如果我设置 key7item list ,则仅当响应中的所有项目均为" key3":" type_two"。

如果它具有数组或对象,我可以将newtonsoft Parser配置为解析数组吗?

简短答案 - 如果您可以避免,请不要这样做。您可以通过给数组来一个不同的关键名称来节省很多复杂性。

长答案,您可以做到,但这很痛苦。如果有人知道如何处理,而又不重新序列化生成的J-Object(即将JSON与供应对象保持在一起),我很想知道:

private static readonly string Json = "[rn  {rn    "key1": "value",rn    "key2": "value",rn    "key3": "TYPE_ONE",rn    "extraData": {rn      "key4": "value",rn      "key5": "value",rn      "key6": {rn        "key7": {rn          "key8": "value",rn          "key9": "value",rn          "key10": "value"rn        }rn      }rn    }rn  },rn  {rn    "key1": "value",rn    "key2": "value",rn    "key3": "TYPE_TWO",rn    "extraData": {rn      "key4": "value",rn      "key5": "value",rn      "key6": {rn        "key7": [rn          {rn            "key8": "value",rn            "key9": "value",rn            "key10": "value"rn          },rn          {rn            "key8": "value1",rn            "key9": "value1",rn            "key10": "value1"rn          },rn          {rn            "key8": "value2",rn            "key9": "value2",rn            "key10": "value2"rn          }rn        ]rn      }rn    }rn  }rn]";
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; }
}

相关内容

  • 没有找到相关文章

最新更新