【问题标题】:how to parse nested JSON response asp.net (vb.net)如何解析嵌套的 JSON 响应 asp.net (vb.net)
【发布时间】:2014-01-10 13:42:52
【问题描述】:

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

这是一个:

{
    "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
    }
}

来自一个站点,另一个来自这里:

{
    "result": "success",
    "return": {
        "high": {
            "value": "745.00000",
            "value_int": "74500000",
            "display": "$745.00",
            "display_short": "$745.00",
            "currency": "USD"
        },
        "low": {
            "value": "610.00000",
            "value_int": "61000000",
            "display": "$610.00",
            "display_short": "$610.00",
            "currency": "USD"
        },
        "avg": {
            "value": "664.21299",
            "value_int": "66421299",
            "display": "$664.21",
            "display_short": "$664.21",
            "currency": "USD"
        },
        "vwap": {
            "value": "658.47213",
            "value_int": "65847213",
            "display": "$658.47",
            "display_short": "$658.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": "$645.00",
            "display_short": "$645.00",
            "currency": "USD"
        },
        "last_orig": {
            "value": "645.00000",
            "value_int": "64500000",
            "display": "$645.00",
            "display_short": "$645.00",
            "currency": "USD"
        },
        "last_all": {
            "value": "645.00000",
            "value_int": "64500000",
            "display": "$645.00",
            "display_short": "$645.00",
            "currency": "USD"
        },
        "last": {
            "value": "645.00000",
            "value_int": "64500000",
            "display": "$645.00",
            "display_short": "$645.00",
            "currency": "USD"
        },
        "buy": {
            "value": "638.36000",
            "value_int": "63836000",
            "display": "$638.36",
            "display_short": "$638.36",
            "currency": "USD"
        },
        "sell": {
            "value": "644.98500",
            "value_int": "64498500",
            "display": "$644.99",
            "display_short": "$644.99",
            "currency": "USD"
        },
        "item": "BTC",
        "now": "1387644090735676"
    }
}

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

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

【问题讨论】:

  • 啊!这是节省时间的一件事:json2csharp.com
  • 试过了,但是无法取出数据 Dim jss = New JavaScriptSerializer() Dim ot As Object = oReturn(0) Dim oReturn = jss.Deserialize(Of Object)(httpdata)跨度>
  • 有人吗?这是反序列化 JSON 字符串的错误方法吗?

标签: c# asp.net json vb.net json.net


【解决方案1】:

好的,明白了。有很多不同的方法可以做到这一点,但首先我必须正确地创建类才能让它工作。我去了 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

重要! - 注意“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

使用 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

本例中输入的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}}

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

【讨论】:

  • 看起来你已经掌握了窍门。 json2csharp.com 上的一个注释——它是一个很好的起点,但正如您可能注意到的那样,它无法识别您可以重用同一个类来实现多种用途的情况。你的第二个例子就是一个很好的例子:json2csharp 为每个不同的属性生成一个单独的类,highlowavg,即使它们显然都具有相同的结构。所以只是要记住一些事情。很多时候你可以简化它给你的东西。
【解决方案2】:

对于您的第一个示例,如果您的类看起来像这样(由 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; }
}

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

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

对于第二个例子,你可以这样定义你的类:

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

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

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

【讨论】:

  • 谢谢布赖恩!抱歉,在您发布答案的同时,我仍在格式化我的发现。我是 JSON 菜鸟,但正在学习!
猜你喜欢
  • 2016-01-28
  • 1970-01-01
  • 1970-01-01
  • 2020-02-04
  • 1970-01-01
  • 2018-07-15
  • 2016-09-03
  • 2023-03-20
  • 2017-04-12
相关资源
最近更新 更多