C# 读取 JSON 字符串作为键值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18185967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:29:59  来源:igfitidea点击:

Read JSON string as key value

c#jsonjsonfx

提问by Ashwani K

I have following json:

我有以下json:

{   
    "serverTime": "2013-08-12 02:45:55,558",
    "data": [
        {
            "key1": 1,
            "key2": {},
            "key3": {
                "key4": [
                    ""
                ],
                "key5": "test2"
            },
            "key7": 0
        },
        {
            "key8": 1,
            "key9": {},
            "key10": {
                "key4": [
                    ""
                ],
                "key9": "test2"
            },
            "key11": 0
        }
    ] 

}

I want to get values as key value pair. Something like:

我想获取值作为键值对。就像是:

jsonObject[data][0]

should give first item of the dataarray.

应该给出数据数组的第一项。

I am using JSONFx.net. But it gives strongly typed objects. I do not want that. Is there any way to parse JSON as key value as I mentioned earlier?

我正在使用 JSONFx.net。但它提供了强类型对象。我不要那个。有没有什么方法可以像我之前提到的那样将 JSON 解析为键值?

Thanks

谢谢

采纳答案by Alex Filipovici

Try this:

尝试这个:

using System;
using System.IO;
using Newtonsoft.Json;

class Program
{
    static void Main(string[] args)
    {
        var json = File.ReadAllText("input.txt");
        var a = new { serverTime = "", data = new object[] { } };
        var c = new JsonSerializer();
        dynamic jsonObject = c.Deserialize(new StringReader(json), a.GetType());
        Console.WriteLine(jsonObject.data[0]);
    }
}

回答by Nirmal

First create classes to parse string

首先创建类来解析字符串

public class Key2
{
}

public class Key3
{
    public List<string> key4 { get; set; }
    public string key5 { get; set; }
}

public class Key9
{
}

public class Key10
{
    public List<string> key4 { get; set; }
    public string key9 { get; set; }
}

public class Datum
{
    public int key1 { get; set; }
    public Key2 key2 { get; set; }
    public Key3 key3 { get; set; }
    public int key7 { get; set; }
    public int? key8 { get; set; }
    public Key9 key9 { get; set; }
    public Key10 key10 { get; set; }
    public int? key11 { get; set; }
}

public class RootObject
{
    public string serverTime { get; set; }
    public List<Datum> data { get; set; }
}

add reference of Newtonsoft.Json.dll

添加 Newtonsoft.Json.dll 的引用

RootObject obj = JsonConvert.DeserializeObject<RootObject>(jsonData);

then you can access values .

然后你可以访问 values 。

回答by Sameer Singh

If you're not averse to using Json.NET, you can do this:

如果您不反对使用Json.NET,您可以这样做:

var jsonString = @"
{   
    ""serverTime"": ""2013-08-12 02:45:55,558"",
    ""data"": [
        {
            ""key1"": 1,
            ""key2"": {},
            ""key3"": {
                ""key4"": [
                    """"
                ],
                ""key5"": ""test2""
            },
            ""key7"": 0
        },
        {
            ""key8"": 1,
            ""key9"": {},
            ""key10"": {
                ""key4"": [
                    """"
                ],
                ""key9"": ""test2""
            },
            ""key11"": 0
        }
    ] 
}";

var jsonResult = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(jsonString);
var firstItem = jsonResult["data"][0];

firstItemwould be an array of the first item in the dataarray:

firstItem将是数组中第一项的data数组:

Demo result

演示结果

Hope this helps.

希望这可以帮助。

回答by Base33

If you want to do this without third party libraries then do:

如果您想在没有第三方库的情况下执行此操作,请执行以下操作:

I would use the following code:

我会使用以下代码:

var deserializer = new JavaScriptSerializer();
var someObject = deserializer.DeserializeObject(json);

string serverTime = someObject["serverTime"].ToString();
Dictionary<string, int> data = someObject["data"] as Dictionary<string, int>;

Give it a go.

搏一搏。

Edit: You may need to change the last line to:

编辑:您可能需要将最后一行更改为:

Dictionary<string, int?> data = someObject["data"] as Dictionary<string, int?>;