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

时间:2013-12-22 00:15:19

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

我有一个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字符串(至少示例如此)。它们显示数组,但这些不是数组。

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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服务。