c# - 如何反序列化 JArray?

标签 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);
                }
            }

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

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

最佳答案

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

关于c# - 如何反序列化 JArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55872591/

相关文章:

c# - 如果 ASP.NET MVC 表单已失效,如何保留字段值?

c - 在C中平均划分一个数组

java - 使用分而治之的方法找到最大值和最小值

javascript - 如何将 JSON 值发送到 FullCalendar 并在日历中显示事件

c# - signalR 和 CustomTypeConverter

c# - EF Core自定义扩展查询,无法翻译LINQ表达式

c# - 在代码中使用 Hangfire job id

arrays - swift 2.2 : cannot convert value of type '[B]' to specified type '[A]'

ruby-on-rails - 如何确保 Rails Controller 将发布到它的数据处理为 JSON?

javascript - 如何动态地将元素以键值格式推送到数组