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)

要分析的代码:

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相关问答推荐

如何在 onClick 事件处理程序中识别在同一 map 上绘制的多个多边形中的哪个(使用 react-leaflet)被单击?

N1QL 聚合查询 Couchbase

如何在 asp.net core 3 中设置 json 序列化程序设置?

消息通知产生“此内容无法显示”

在 JSON 字符串中react i18n 换行符

是否可以通过适当的 JavaScript 字符串转义来利用 JSON 响应进行 XSS 攻击?

Gson:@Expose 与 @SerializedName

JSON.parse 返回字符串而不是对象

将 YAML 文件转换为 Python JSON 对象

对 kotlin 数据类使用 Jackson @JsonProperty 注释

JSON Jackson 将不同的键解析到同一个字段中

pandas.io.json.json_normalize 带有非常嵌套的 json

c# & newtonsoft 中的 JSON 日期和日期时间序列化

json.loads 可以忽略尾随逗号吗?

AWS:如何修复 S3 事件用“+”登录 json 中的对象键名替换空格

如何将 JSON 字符串转换为 Java 对象列表?

在 Ubuntu 10.04 上安装 jq JSON 处理器

如何使用 Serde 反序列化带有自定义函数的可选字段?

TypeError:JSON 对象必须是 str,而不是 'bytes'

按键值过滤 JSON