如何使用JSON.NET反序列化?

时间:2022-02-03 17:02:24

How do I setup Newtonsoft Json.net to deserialize this text into a .NET object?

如何设置Newtonsoft Json.net将此文本反序列化为.NET对象?

[
    [
        "US\/Hawaii", 
        "GMT-10:00 - Hawaii"
    ], 
    [
        "US\/Alaska", 
        "GMT-09:00 - Alaska"
    ], 
]

For bonus points, what is this kind of structure called in Json. I tried looking for anonymous objects, but didn't have any luck.

对于奖励积分,在Json中称为什么样的结构。我试着寻找匿名对象,但没有任何运气。

3 个解决方案

#1


5  

JSON.Net uses JArray to allow these to be parsed - see:

JSON.Net使用JArray来解析这些 - 请参阅:

#2


6  

This JSON string (or almost, it will be a valid JSON after you fix it and remove the trailing comma, as right now it's invalid) represents an array of arrays of strings. It could be easily deserialized into a string[][] using the built into .NET JavaScriptSerializer class:

这个JSON字符串(或者几乎在修复它之后它将是一个有效的JSON并删除尾随的逗号,因为它现在无效)表示一个字符串数组的数组。使用内置的.NET JavaScriptSerializer类可以很容易地将其反序列化为string [] []:

using System;
using System.Web.Script.Serialization;

class Program
{

    static void Main()
    {
        var json = 
@"[
    [
        ""US\/Hawaii"", 
        ""GMT-10:00 - Hawaii""
    ], 
    [
        ""US\/Alaska"", 
        ""GMT-09:00 - Alaska""
    ]
]";
        var serializer = new JavaScriptSerializer();
        var result = serializer.Deserialize<string[][]>(json);
        foreach (var item in result)
        {
            foreach (var element in item)
            {
                Console.WriteLine(element);
            }
        }
    }
}

and the exactly same result could be achieved with JSON.NET using the following:

使用以下内容可以使用JSON.NET实现完全相同的结果:

var result = JsonConvert.DeserializeObject<string[][]>(json);

#3


0  

To see a blog entry detailing how to serialize and deserialize between .NET and JSON, check this out. I found it really helpful.

要查看详细介绍如何在.NET和JSON之间进行序列化和反序列化的博客条目,请查看此内容。我发现它真的很有帮助。

#1


5  

JSON.Net uses JArray to allow these to be parsed - see:

JSON.Net使用JArray来解析这些 - 请参阅:

#2


6  

This JSON string (or almost, it will be a valid JSON after you fix it and remove the trailing comma, as right now it's invalid) represents an array of arrays of strings. It could be easily deserialized into a string[][] using the built into .NET JavaScriptSerializer class:

这个JSON字符串(或者几乎在修复它之后它将是一个有效的JSON并删除尾随的逗号,因为它现在无效)表示一个字符串数组的数组。使用内置的.NET JavaScriptSerializer类可以很容易地将其反序列化为string [] []:

using System;
using System.Web.Script.Serialization;

class Program
{

    static void Main()
    {
        var json = 
@"[
    [
        ""US\/Hawaii"", 
        ""GMT-10:00 - Hawaii""
    ], 
    [
        ""US\/Alaska"", 
        ""GMT-09:00 - Alaska""
    ]
]";
        var serializer = new JavaScriptSerializer();
        var result = serializer.Deserialize<string[][]>(json);
        foreach (var item in result)
        {
            foreach (var element in item)
            {
                Console.WriteLine(element);
            }
        }
    }
}

and the exactly same result could be achieved with JSON.NET using the following:

使用以下内容可以使用JSON.NET实现完全相同的结果:

var result = JsonConvert.DeserializeObject<string[][]>(json);

#3


0  

To see a blog entry detailing how to serialize and deserialize between .NET and JSON, check this out. I found it really helpful.

要查看详细介绍如何在.NET和JSON之间进行序列化和反序列化的博客条目,请查看此内容。我发现它真的很有帮助。