• 0

Nested JSON data


Question

I want to consume the following JSON data structure using a VB.NET handler. The handler is working on the root keys - how do I deal with Sub > Sub-1, Sub > Sub-2, Another-Sub > Sub-3

{
    "ID": id,
    "ParentId": parentId,
    "Sub":
        [{
           "Sub-1":
                [{
                   "Data":"Test",
                   "MoreData":"MoreTestData"
                }],
            "Sub-2":
                [{
                   "Data":"Test 2",
                   "MoreData":"More Test Data 2"
                }],
        }],
    "Another-Sub":
        [{
           "Sub-3":
                [{
                   "Data":"Test 2",
                   "MoreData":"More Test Data",
                   "EvenMoreDate":"Even More Data"
                }],
           "Sub-4":
                [{
                   "Data":"Test 2",
                   "MoreData":"More Test Data",
                   "EvenMoreDate":"Even More Data"
                }],
        }],
}

 

My Handler code is as follows:

Dim js As New JavaScriptSerializer()
    Dim data As DataClass = js.Deserialize(Of DataClass)(json)
    
    Public Class DataClass
        Public Property ID() As String
            Get
                Return _ID
            End Get
            Set(value As String)
                _ID = value
            End Set
        End Property
        Private _ID As String

        etc etc
        
    End Class

    Public Class Sub
        Public Property Data() As String
    End Class



How can I access tier 2 / 3 json data?

All help appreciated - hopefully I've described my issue satisfactorily.

 

 

 

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Hi,

There are a plethora of ways to work with JSON on multi-levels, however you'll really want to add JSON.NET (Website Link) or similar to your application and utilise that library rather than "hacking" at it with a JavaScriptSerializer.
This will give you an easy way of serialising/deserialising and even implementing LINQ-To-JSON.

Enjoy!

Link to comment
Share on other sites

  • 0

Thanks Kami .. I was a little reluctant to use a third party solution seeing as there is support within the framework. I'll have a look and see if that helps any.

Appreciate the response.

Link to comment
Share on other sites

  • 0

Thanks Kami .. I was a little reluctant to use a third party solution seeing as there is support within the framework. I'll have a look and see if that helps any.

Appreciate the response.

Newtonsoft has been a standard .NET library for at least the last year or two.

 

This is a standard web api project:

 

Capture.PNG

Edited by adrynalyne
Link to comment
Share on other sites

  • 0

First thing I always do is creating a multidimensional array from a json string so it's easier to work with :p

Link to comment
Share on other sites

This topic is now closed to further replies.