MC9000
MC9000

Reputation: 2403

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

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).

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": "$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"
    }
}

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.

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.

Upvotes: 1

Views: 7662

Answers (2)

MC9000
MC9000

Reputation: 2403

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):

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:

   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):

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:

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

Upvotes: 1

Brian Rogers
Brian Rogers

Reputation: 129697

For your first example, if you have classes that look like this (generated by 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:

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

Upvotes: 2

Related Questions