I'm trying to read a Json string in C#, but I'm having trouble figuring out just how to parse the string into C#. Say I have the following Json string

[
    {
        "AppName": {
            "Description": "Lorem ipsum dolor sit amet",
            "Value": "1"
        },
        "AnotherAppName": {
            "Description": "consectetur adipisicing elit",
            "Value": "String"
        },
        "ThirdAppName": {
            "Description": "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
            "Value": "Text"
        },
        "Application": {
            "Description": "Ut enim ad minim veniam",
            "Value": "100"
        },
        "LastAppName": {
            "Description": "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat",
            "Value": "ZZZ"
        }
    }
]

I want to parse that into an arraylist or dictionary, using a format like

descriptionList["AppName"] = "Lorem ipsum dolor sit amet";
valueList["AppName"] = "1";

I've been toying around with Json.Net but the examples I've seen don't give me a clear idea of how I should do this. What's the best way to achieve this? Cant this be done like in jQuery, using a foreach statement?

推荐答案

I'm using Json.net in my project and it works great. In you case, you can do this to parse your json:

EDIT: I changed the code so it supports reading your json file (array)

Code to parse:

void Main()
{
    var json = System.IO.File.ReadAllText(@"d:\test.json");

    var objects = JArray.Parse(json); // parse as array  
    foreach(JObject root in objects)
    {
        foreach(KeyValuePair<String, JToken> app in root)
        {
            var appName = app.Key;
            var description = (String)app.Value["Description"];
            var value = (String)app.Value["Value"];

            Console.WriteLine(appName);
            Console.WriteLine(description);
            Console.WriteLine(value);
            Console.WriteLine("\n");
        }
    }
}

Output:

AppName
Lorem ipsum dolor sit amet
1


AnotherAppName
consectetur adipisicing elit
String


ThirdAppName
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
Text


Application
Ut enim ad minim veniam
100


LastAppName
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat
ZZZ

BTW, you can use LinqPad to test your code, easier than creating a solution or project in Visual Studio I think.

Json相关问答推荐

使用动态和可变JSON的图表多条

无法从MongoDB集合中检索正确的文档

错误:在 NX 工作区中找不到模块../swagger.json

Jolt Spec 跳过数组中的第一个元素

使用 jq 和 awk 拆分大型 JSON 文件

Scala - 在构建 Json 时无法删除 Key -> value "{}" 大括号的双引号

使用带有逗号的字段名称构建 struct

如何在 Dart 中与多个 map (字典)相交

JSON RPC - 什么是id?

Qt使用QJsonDocument、QJsonObject、QJsonArray解析JSON

在 Http Header 中使用 Json 字符串

使用 API 搜索维基百科

有没有办法使用 Jackson 将 Map 转换为 JSON 表示而不写入文件?

TypeError: b'1' 不是 JSON 可序列化的

如何访问 JSON 对象数组的第一个元素?

使用 JSON.Net 的 C# 到 JSON 序列化

[__NSCFNumber 长度]:发送到实例 UITableView 的无法识别的 Select 器

Protocol Buffer vs Json - 何时 Select 一个而不是另一个

你如何在 Arrays of Arrays 上 OPENJSON

Microsoft.Net.Http 与 Microsoft.AspNet.WebApi.Client