如何解析嵌套的 JSON 响应 asp.net (vb.net)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20724895/
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-09-17 16:13:57  来源:igfitidea点击:

how to parse nested JSON response asp.net (vb.net)

c#asp.netjsonvb.netjson.net

提问by MC9000

I have a JSON response that I need to parse into an object in ASP.Net (Vb.net or c#), but I don't see any examples for a nested response string and how to parse (only simple value pairs).

我有一个 JSON 响应,我需要将其解析为 ASP.Net(Vb.net 或 c#)中的对象,但我没有看到任何嵌套响应字符串的示例以及如何解析(仅简单值对)。

Here's one:

这是一个:

{
    "ticker": {
        "high": 3.494,
        "low": 2.9,
        "avg": 3.197,
        "vol": 463260.58724,
        "vol_cur": 143878.12481,
        "last": 2.924,
        "buy": 2.959,
        "sell": 2.925,
        "updated": 1387635241,
        "server_time": 1387635242
    }
}

from one site, and another one here:

从一个站点,另一个站点在这里:

{
    "result": "success",
    "return": {
        "high": {
            "value": "745.00000",
            "value_int": "74500000",
            "display": "5.00",
            "display_short": "5.00",
            "currency": "USD"
        },
        "low": {
            "value": "610.00000",
            "value_int": "61000000",
            "display": "0.00",
            "display_short": "0.00",
            "currency": "USD"
        },
        "avg": {
            "value": "664.21299",
            "value_int": "66421299",
            "display": "4.21",
            "display_short": "4.21",
            "currency": "USD"
        },
        "vwap": {
            "value": "658.47213",
            "value_int": "65847213",
            "display": "8.47",
            "display_short": "8.47",
            "currency": "USD"
        },
        "vol": {
            "value": "29333.04107565",
            "value_int": "2933304107565",
            "display": "29,333.04?BTC",
            "display_short": "29,333.04?BTC",
            "currency": "BTC"
        },
        "last_local": {
            "value": "645.00000",
            "value_int": "64500000",
            "display": "5.00",
            "display_short": "5.00",
            "currency": "USD"
        },
        "last_orig": {
            "value": "645.00000",
            "value_int": "64500000",
            "display": "5.00",
            "display_short": "5.00",
            "currency": "USD"
        },
        "last_all": {
            "value": "645.00000",
            "value_int": "64500000",
            "display": "5.00",
            "display_short": "5.00",
            "currency": "USD"
        },
        "last": {
            "value": "645.00000",
            "value_int": "64500000",
            "display": "5.00",
            "display_short": "5.00",
            "currency": "USD"
        },
        "buy": {
            "value": "638.36000",
            "value_int": "63836000",
            "display": "8.36",
            "display_short": "8.36",
            "currency": "USD"
        },
        "sell": {
            "value": "644.98500",
            "value_int": "64498500",
            "display": "4.99",
            "display_short": "4.99",
            "currency": "USD"
        },
        "item": "BTC",
        "now": "1387644090735676"
    }
}

I downloaded Json.Net (looks good), but it looks like it only supports non-nested JSON strings (at least the examples do). They show arrays, but these are not arrays as such.

我下载了 Json.Net(看起来不错),但看起来它只支持非嵌套 JSON 字符串(至少示例支持)。它们显示数组,但这些不是数组。

I thought about doing a sort of manual parsing using string manipulation and regular expressions, but would rather have something I can reuse. Just not sure where to start.

我想过使用字符串操作和正则表达式进行某种手动解析,但更希望有一些我可以重用的东西。只是不知道从哪里开始。

回答by Brian Rogers

For your first example, if you have classes that look like this (generated by json2csharp.com):

对于您的第一个示例,如果您有如下所示的类(由 json2csharp.com 生成):

public class RootObject
{
    public Ticker ticker { get; set; }
}

public class Ticker
{
    public double high { get; set; }
    public double low { get; set; }
    public double avg { get; set; }
    public double vol { get; set; }
    public double vol_cur { get; set; }
    public double last { get; set; }
    public double buy { get; set; }
    public double sell { get; set; }
    public int updated { get; set; }
    public int server_time { get; set; }
}

then you can deserialize into them like this using Json.Net:

然后你可以使用 Json.Net 像这样反序列化它们:

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

For the second example, you could define your classes like this:

对于第二个示例,您可以像这样定义类:

public class RootObject2
{
    public string result { get; set; }
    public Return @return { get; set; }
}

public class Return
{
    public Item high { get; set; }
    public Item low { get; set; }
    public Item avg { get; set; }
    public Item vwap { get; set; }
    public Item vol { get; set; }
    public Item last_local { get; set; }
    public Item last_orig { get; set; }
    public Item last_all { get; set; }
    public Item last { get; set; }
    public Item buy { get; set; }
    public Item sell { get; set; }
    public string item { get; set; }
    public string now { get; set; }
}

public class Item
{
    public string value { get; set; }
    public string value_int { get; set; }
    public string display { get; set; }
    public string display_short { get; set; }
    public string currency { get; set; }
}

and deserialize in the same way:

并以相同的方式反序列化:

RootObject2 obj = JsonConvert.DeserializeObject<RootObject2>(json2);

回答by MC9000

OK, got it figured out. There's many different ways to do this, but first I had to create classes PROPERLY to get this to work. I went to json2csharp.com and pasted in the URL that returns JSON (or, alternatively, paste a JSON string in) - this creates your classes automatically (you can type them out manually too, of course), which is nice. In my first example, the classes look like the following (in VB.Net):

好的,搞清楚了。有很多不同的方法可以做到这一点,但首先我必须正确创建类才能使其工作。我去了 json2csharp.com 并粘贴了返回 JSON 的 URL(或者,粘贴一个 JSON 字符串)——这会自动创建你的类(当然,你也可以手动输入它们),这很好。在我的第一个示例中,类如下所示(在 VB.Net 中):

Namespace BTCE
#Region "BTCE response classes"
    Public Class Ticker
        Public Property high() As Double
            Get
                Return m_high
            End Get
            Set(value As Double)
                m_high = value
            End Set
        End Property
        Private m_high As Double
        Public Property low() As Double
            Get
                Return m_low
            End Get
            Set(value As Double)
                m_low = value
            End Set
        End Property
        Private m_low As Double
        Public Property avg() As Double
            Get
                Return m_avg
            End Get
            Set(value As Double)
                m_avg = value
            End Set
        End Property
        Private m_avg As Double
        Public Property vol() As Double
            Get
                Return m_vol
            End Get
            Set(value As Double)
                m_vol = value
            End Set
        End Property
        Private m_vol As Double
        Public Property vol_cur() As Double
            Get
                Return m_vol_cur
            End Get
            Set(value As Double)
                m_vol_cur = value
            End Set
        End Property
        Private m_vol_cur As Double
        Public Property last() As Double
            Get
                Return m_last
            End Get
            Set(value As Double)
                m_last = value
            End Set
        End Property
        Private m_last As Double
        Public Property buy() As Double
            Get
                Return m_buy
            End Get
            Set(value As Double)
                m_buy = value
            End Set
        End Property
        Private m_buy As Double
        Public Property sell() As Double
            Get
                Return m_sell
            End Get
            Set(value As Double)
                m_sell = value
            End Set
        End Property
        Private m_sell As Double
        Public Property updated() As Integer
            Get
                Return m_updated
            End Get
            Set(value As Integer)
                m_updated = value
            End Set
        End Property
        Private m_updated As Integer
        Public Property server_time() As Integer
            Get
                Return m_server_time
            End Get
            Set(value As Integer)
                m_server_time = value
            End Set
        End Property
        Private m_server_time As Integer
    End Class

    Public Class RootObject
        Public Property ticker() As Ticker
            Get
                Return m_ticker
            End Get
            Set(value As Ticker)
                m_ticker = value
            End Set
        End Property
        Private m_ticker As Ticker
    End Class
#End Region

End Namespace

IMPORTANT! - note the "RootObject" class get/sets the ticker object Using the JavaScriptSerializer, the code looks like so:

重要的!- 注意“RootObject”类使用 JavaScriptSerializer 获取/设置股票代码对象,代码如下所示:

   Dim jss = New JavaScriptSerializer()
   Dim oReturn As BTCE.RootObject = jss.Deserialize(Of BTCE.RootObject)(httpdata)
   Dim avg As String = oReturn.ticker.avg
   Dim high as String = oReturn.ticker.high
    ... and so forth

Using the Newtonsoft.Json library (json.net):

使用 Newtonsoft.Json 库 (json.net):

Dim ro As BTCE.RootObject = JsonConvert.DeserializeObject(Of BTCE.RootObject)(httpdata)
Dim avg As String = ro.ticker.avg
Dim high As String = ro.ticker.high
... and so forth

The httpdata string that was input in this case is:

在这种情况下输入的 httpdata 字符串是:

{"ticker":{"high":3.494,"low":2.9,"avg":3.197,"vol":463260.58724,"vol_cur":143878.12481,"last":2.924,"buy":2.959,"sell":2.925,"updated":1387635241,"server_time":1387635242}}

This is my first foray into converting JSON to something usable, but after having very little luck in finding complex examples (nested JSON) on the Internet, I get it now and hope this helps DotNetters everywhere. In my example, I have an app that polls a website for JSON data, converts it into a usable object, does some processing and, ultimately outputs (more) JSON data (as well as XML and other formats) as a WCF web service.

这是我第一次尝试将 JSON 转换为可用的东西,但在互联网上找到复杂示例(嵌套 JSON)的运气很少之后,我现在明白了,希望这对各地的 DotNetters 有所帮助。在我的示例中,我有一个应用程序可以轮询网站以获取 JSON 数据,将其转换为可用对象,进行一些处理,并最终将(更多)JSON 数据(以及 XML 和其他格式)输出为 WCF Web 服务。