如何反序列化JArray?

时间:2019-04-26 17:41:09

标签: c# arrays json

我需要反序列化并获取JSON(Jarray)字符串的子值

string initialJson = File.ReadAllText(Application.StartupPath + "\\Coda.json");

在这种情况下是:

[
    "Scan",
    [
        "4eba277ef42fe2aeb6e3ac17ba00b784",
        {
            "ID":0,
            "Link":"https://www.virustotal.com/file/9416fbb8043d6eb6b544a79e51494a9181c/analysis/1556298405/",
            "ScanID":"9416fbb804db12002c39d6eb6b544a79e51494a9181c-1556298405"
        }
    ],
    [
        "80434f3307d5f66ce3548c6a3c735f34",
        {
            "ID":1,
            "Link":"https://www.virustotal.com/file/63fe7b058c20f39a579a94e56c85638ba7cd6f9f/analysis/1556298411/",
            "ScanID":"63fe7b058c20f3524760728294e56c85638ba7cd6f9f-1556298411"
        }
    ]
]

并使用:

进行转换
JArray jsonArray = JsonConvert.DeserializeObject<JArray>(initialJson);

在此之后,我开始了一个foreach:

foreach (JObject item in jsonArray.Children<JObject>())
            {
                foreach (JProperty element in item.Properties())
                {
                    MessageBox.Show(element.Name + " " + element.Value);
                }
            }

但是什么也没出现,没有消息框,没有错误,为什么?

我需要获取所有值或第一个值并将其删除

1 个答案:

答案 0 :(得分:1)

JArray中的JArray,而不是JToken对象:

foreach (JArray item in jsonArray.Children<JArray>())
{
    foreach (JToken element in item.Children())
    {
        foreach(JProperty prop in element){

            prop.Value.Dump(prop.Name);

        }
    }
}

enter image description here