Convert JSON to Array in VB.NET

I have this code

Dim x As String
x = "{'books':[{'title':'HarryPotter','pages':'134'}]}"

what i want to do is convert it to an array like in php using function json_decode(x,TRUE or FALSE)

+4
source share
2 answers

Your string xdoes not contain an array, but a single JSON object.

Just use the JSON library, for example Json.NET, to parse your string:

Dim x = "{'books':[{'title':'HarryPotter','pages':'134'}]}"

Dim result = JsonConvert.DeserializeObject(x)
Console.WriteLine(result("books")(0)("title") & " - " & result("books")(0)("pages"))

Output:

HarryPotter - 134

+6
source

@Professor Haseeb You may forget to add the following to @Dominic Kexel's solution:

Imports Newtonsoft.Json

Or use:

Dim result = Newtonsoft.Json.JsonConvert.DeserializeObject(x)
+2
source

Source: https://habr.com/ru/post/1536563/


All Articles