4

我有以下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
        }
    ] 

}

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

jsonObject[data][0]

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

我正在使用 JSONFx.net。但它提供了强类型对象。我不要那个。如前所述,有什么方法可以将 JSON 解析为键值吗?

谢谢

4

4 回答 4

6

尝试这个:

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]);
    }
}
于 2013-08-12T12:48:19.890 回答
3

如果你不反对使用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];

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

演示结果

希望这可以帮助。

于 2013-08-12T12:36:21.683 回答
1

首先创建类来解析字符串

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; }
}

添加 Newtonsoft.Json.dll 的引用

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

然后您可以访问值。

于 2013-08-12T12:23:14.033 回答
1

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

我会使用以下代码:

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

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

搏一搏。

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

Dictionary<string, int?> data = someObject["data"] as Dictionary<string, int?>;
于 2013-08-12T12:45:45.853 回答