Dade Dade - 2 months ago 10
Vb.net Question

How do you read this format of data?

I have the following set of data (much larger, I've cut it down)

[{
"title": "Brave",
"year": "2012",
"imdbid": "tt1217209",
"scid": "2"
}, {
"title": "The Watch",
"year": "2012",
"imdbid": "tt1298649",
"scid": "1"
}]


I would like to be able to get the "title", for example Brave by finding scid : 2.

This isn't similar to other Json format that I am familiar with, because the other son has a parent, and child node, but this doesn't.

What would I need to do to be able to get the data I need?
I just need to be able to read the value in vb.net

EDIT: I belive this is Javascript Json format?

Answer Source

It seems a pretty standard and simple json to me.
You can get your object with this straightforward code.

First you have a class like this

Class myObj

    Public Property Title As String
    Public Property year As Integer 
    Public Property imdbid As String
    Public Property scid As Integer

End Class

Then you get it with

Sub Main()

    Dim input = File.ReadAllText("d:\temp\test2.json")
    Dim list = JsonConvert.DeserializeObject(Of List(Of myObj))(input)
    Dim obj = list.FirstOrDefault(Function(x) x.scid = 2)
    Console.WriteLine(obj.Title)
End Sub

(Of course you need to have a json parse library referenced in your project like newtonsoft.json as explained in other answer)