C#JSON反序列化动态变量

时间:2018-03-22 14:54:08

标签: c# json

我是Json的新手,我正在尝试解析来自一家供应商的以下示例:

{
  "Nodes": [ "ID1", "ID2", "ID3", "IDxx" ],
  "Results": {
    "ID1": {
      "ID2": {
        "value1": "example1",
        "value2": "exempleHexa"
      },
      "ID3": {
        "value1": "example2",
        "value2": "exempleHexa"
      }
    },
    "ID3": {
      "ID1": {
        "value1": "example3",
        "value2": "exempleHexa"
      },
      "ID2": {
        "value1": "example4",
        "value2": "exempleHexa"
      },
      "ID3": {
        "value1": "example5",
        "value2": "exempleHexa"
      }
    }
  }
}

我尝试使用这些行来访问Value1,但它总是返回null:

    var json = JsonConvert.DeserializeObject<dynamic>(JsonData);
    var data = ((JObject)json.Results).Children().ToArray();

    foreach (var item in data)
    {
        var childss = item.Children().ToArray();

        foreach (var item2 in childss)
        {
            var sub = JsonConvert.DeserializeObject<dynamic>(item2.ToString());
            string hmm = sub.value1;
        }
    }

有人会有想法吗?感谢。

4 个答案:

答案 0 :(得分:0)

试试这个:

public class ID2
{
    public string value1 { get; set; }
    public string value2 { get; set; }
}

public class ID3
{
    public string value1 { get; set; }
    public string value2 { get; set; }
}

public class ID1
{
    public ID2 ID2 { get; set; }
    public ID3 ID3 { get; set; }
}

public class ID12
{
    public string value1 { get; set; }
    public string value2 { get; set; }
}

public class ID22
{
    public string value1 { get; set; }
    public string value2 { get; set; }
}

public class ID33
{
    public string value1 { get; set; }
    public string value2 { get; set; }
}

public class ID32
{
    public ID12 ID1 { get; set; }
    public ID22 ID2 { get; set; }
    public ID33 ID3 { get; set; }
}

public class Results
{
    public ID1 ID1 { get; set; }
    public ID32 ID3 { get; set; }
}

public class RootObject
{
    public List<string> Nodes { get; set; }
    public Results Results { get; set; }
}

然后是这样的事情:

var data = JsonConvert.DeserializeObject<RootObject>(JsonData);

您将获得根对象中的所有内容。

答案 1 :(得分:0)

JObject已经是动态的和反序列化的,因此您无需重新序列化它们并再次反序列化它们。

您的输入结构有另一层对象,您似乎正在跳过它。例如,如果您想要value1的{​​{1}}值,则需要深入挖掘一下:

Results.ID1.ID2

您可以显着清理语法:

var json = JsonConvert.DeserializeObject<dynamic>(JsonData);
var data = ((JObject)json.Results).Properties().Values().Cast<JObject>().ToArray();

foreach (var item in data)
{
    var childss = item.Properties().Values().ToArray();
    foreach (dynamic item2 in childss)
    {
        string hmm = item2.value1;
        Console.WriteLine(hmm);
    }
}

答案 2 :(得分:0)

Children将始终返回根对象的子项。您需要使用Value属性,然后询问其子项。这段代码应该有效:

var data = ((JObject)json.Results).Children().ToArray();

foreach (JProperty item in data)
{
    var childss = item.Value.Children().ToArray();

    foreach (JProperty item2 in childss)
    {
        dynamic sub = item2.Value;
        string hmm = sub.value1;
    }
}

答案 3 :(得分:0)

你想做这样的事吗?

        string test = "{   \"Nodes\": [ \"ID1\", \"ID2\", \"ID3\", \"IDxx\" ],   \"Results\": {     \"ID1\": {       \"ID2\": {         \"value1\": \"example1\",         \"value2\": \"exempleHexa\"       },       \"ID3\": {         \"value1\": \"example2\",         \"value2\": \"exempleHexa\"       }     },     \"ID3\": {       \"ID1\": {         \"value1\": \"example3\",         \"value2\": \"exempleHexa\"       },       \"ID2\": {         \"value1\": \"example4\",         \"value2\": \"exempleHexa\"       },       \"ID3\": {         \"value1\": \"example5\",         \"value2\": \"exempleHexa\"       }     }   } }";
        dynamic obj = JsonConvert.DeserializeObject(test);
        Console.WriteLine("test output" + obj.Nodes[0]);

只需通过传递它们所在数组的索引来访问数据,如obj.Nodes[0]等等。