-
Mar 5th, 2018, 12:26 PM
#1
Thread Starter
New Member
[RESOLVED] Deserialising nested json arrays?
Below is my json and generated classes using jsonutil.com and my code which is not somehow doing the job because i am just getting rootObject, don't know how to get further. My purpose is to deserialize it and get every event in TreeView nodes. I am kind of confused how can achive all these events in json array.
JSON
Code:
[
{"event":{"id":"28445113","name":"Sheffield Shield","countryCode":"AU","timezone":"GMT","openDate":"2017-10-26T03:00:00.000Z"},"marketCount":1},
{"event":{"id":"28615136","name":"Peshawar Zalmi v Multan Sultans [LM]","timezone":"GMT","openDate":"2018-03-06T16:00:00.000Z"},"marketCount":8},
{"event":{"id":"28611618","name":"ICC World Cup Qualifier","timezone":"GMT","openDate":"2099-01-01T00:00:00.000Z"},"marketCount":2},
{"event":{"id":"28434471","name":"Ireland v Pakistan","timezone":"GMT","openDate":"2018-05-11T10:01:00.000Z"},"marketCount":1},
{"event":{"id":"28618092","name":"Sri Lanka v India (1st T20)","timezone":"GMT","openDate":"2018-03-06T13:30:00.000Z"},"marketCount":5},
{"event":{"id":"28529194","name":"ICC World Twenty20 2020","countryCode":"AU","timezone":"GMT","openDate":"2020-10-24T14:00:00.000Z"},"marketCount":1},
{"event":{"id":"27408637","name":"ICC Cricket World Cup 2019","countryCode":"GB","timezone":"Europe/London","openDate":"2019-05-30T09:30:00.000Z"},"marketCount":1},
{"event":{"id":"28618068","name":"Ireland v Papua New Guinea","timezone":"GMT","openDate":"2018-03-06T07:30:00.000Z"},"marketCount":7},
{"event":{"id":"28618069","name":"Hong Kong v Scotland","timezone":"GMT","openDate":"2018-03-06T07:30:00.000Z"},"marketCount":7},
{"event":{"id":"28618070","name":"UAE v West Indies","timezone":"GMT","openDate":"2018-03-06T07:30:00.000Z"},"marketCount":7},
{"event":{"id":"28618071","name":"Zimbabwe v Afghanistan","timezone":"GMT","openDate":"2018-03-06T07:30:00.000Z"},"marketCount":7},
{"event":{"id":"28594707","name":"New Zealand v England","countryCode":"NZ","timezone":"NZ","openDate":"2018-03-22T01:00:00.000Z"},"marketCount":3},
{"event":{"id":"28586332","name":"New Zealand v England","countryCode":"NZ","timezone":"NZ","openDate":"2018-03-02T21:00:00.000Z"},"marketCount":3},
{"event":{"id":"28617436","name":"New Zealand Women v West Indies Women","countryCode":"NZ","timezone":"NZ","openDate":"2018-03-07T22:00:00.000Z"},"marketCount":5},
{"event":{"id":"28618076","name":"Islamabad United v Lahore Qalandars","timezone":"GMT","openDate":"2018-03-08T11:30:00.000Z"},"marketCount":22},
{"event":{"id":"28618074","name":"Karachi Kings v Quetta Gladiators","timezone":"GMT","openDate":"2018-03-08T16:00:00.000Z"},"marketCount":22},
{"event":{"id":"28618075","name":"Multan Sultans v Quetta Gladiators","timezone":"GMT","openDate":"2018-03-07T16:00:00.000Z"},"marketCount":22},
{"event":{"id":"28102621","name":"Pakistan Super League","timezone":"GMT","openDate":"2099-01-01T00:00:00.000Z"},"marketCount":3},
{"event":{"id":"28615878","name":"New Zealand v England (4th ODI)","countryCode":"NZ","timezone":"GMT","openDate":"2018-03-06T22:00:00.000Z"},"marketCount":36},
{"event":{"id":"27899739","name":"County Championship Div 2 Matches","timezone":"GMT","openDate":"2099-01-01T00:00:00.000Z"},"marketCount":1},
{"event":{"id":"27899740","name":"County Championship Div 1 Matches","timezone":"GMT","openDate":"2099-01-01T00:00:00.000Z"},"marketCount":1},
{"event":{"id":"28609475","name":"South Africa v Australia","countryCode":"GB","timezone":"Europe/London","openDate":"2018-03-04T08:00:00.000Z"},"marketCount":3},
{"event":{"id":"27918802","name":"IPL","timezone":"GMT","openDate":"2099-01-01T00:00:00.000Z"},"marketCount":1},
{"event":{"id":"28612364","name":"Tas Tigers v SA Redbacks","timezone":"GMT","openDate":"2018-03-05T23:30:00.000Z"},"marketCount":1},
{"event":{"id":"28612362","name":"Vic Bushrangers v NSW Blues","timezone":"GMT","openDate":"2018-03-02T23:30:00.000Z"},"marketCount":1},
{"event":{"id":"28589706","name":"South Africa v Australia (1st Test)","timezone":"GMT","openDate":"2018-03-05T08:00:00.000Z"},"marketCount":10},
{"event":{"id":"28615050","name":"Peshawar Zalmi v Multan Sultans","timezone":"GMT","openDate":"2018-03-06T16:00:00.000Z"},"marketCount":24},
{"event":{"id":"28612363","name":"Qld Bulls v WA Warriors","timezone":"GMT","openDate":"2018-03-06T00:00:00.000Z"},"marketCount":1},
{"event":{"id":"28615947","name":"New Zealand v England (4th ODI) [LM]","countryCode":"NZ","timezone":"GMT","openDate":"2018-03-06T22:00:00.000Z"},"marketCount":14}
]
Generated VB.NET Classes
Code:
Public Class anEvent
Public Property id As String
Public Property name As String
Public Property countryCode As String
Public Property timezone As String
Public Property openDate As DateTime
End Class
Public Class Example
Public Property myEvent As anEvent
Public Property marketCount As Integer
End Class
My Code
Code:
Dim json = RichTextBox1.Text
Dim obj As rootObject = JsonConvert.DeserializeObject(Of rootObject)(json)
-
Mar 5th, 2018, 02:09 PM
#2
Re: Deserialising nested json arrays?
The "object" in this case is really just an array. This kind of return is discouraged from REST APIs because parsing is more complex if you have to figure out, "Am I dealing with an object?" before you can start parsing.
Read the JSON like English, and it becomes clear you've only got some of the types you need. It reads:
An array of objects with two properties. "event" is an object, and "marketCount" is an Integer.
So, out of the gate, your class "Example" is wrong. It should look like:
Code:
Public Class Example
Public Property event As anEvent
Public Property marketCount As Integer
End Class
The rest of your class definitions look OK. The part you're mssing is "an array of objects". This JSON represents what VB would call Example(), or "an array of Example objects". So, to deserialize:
Code:
Dim examples() As Example = JsonConvert.DeserializeObject(Of Example())(json)
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Mar 5th, 2018, 04:37 PM
#3
Junior Member
Re: Deserialising nested json arrays?
I had the same problem. This code is working for me perfect. Thanx
-
Mar 5th, 2018, 04:41 PM
#4
Thread Starter
New Member
Re: Deserialising nested json arrays?
Originally Posted by Sitten Spynne
So, out of the gate, your class "Example" is wrong. It should look like:
Code:
Public Class Example
Public Property event As anEvent
Public Property marketCount As Integer
End Class
Well it was giving error on event so I wrapped it up like this [event].
The rest of your class definitions look OK. The part you're mssing is "an array of objects". This JSON represents what VB would call Example(), or "an array of Example objects". So, to deserialize:
Code:
Dim examples() As Example = JsonConvert.DeserializeObject(Of Example())(json)
Thanx. This was the mistake. I am now using this code to populate treeview using every event name.
Code:
Dim json = RichTextBox1.Text
Dim obj() As rootObject = JsonConvert.DeserializeObject(Of rootObject())(json)
For Each o In obj
TreeView1.Nodes.Add(o.event.name)
Next
-
Mar 5th, 2018, 06:20 PM
#5
Re: [RESOLVED] Deserialising nested json arrays?
Right! I forgot. "Event" is a keyword, so to use it as an identifier you have to wrap it in [brackets]. Alternatively, you can use attributes to tell the JSON library that a property corresponds to a differently-named JSON member.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|