Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting JSON to Array in VB.NET

Tags:

vb.net

I have this code

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

what i want to do is to convert it into array like we do in PHP using the json_decode(x,TRUE or FALSE) function

like image 739
Professor Haseeb Avatar asked Dec 14 '22 23:12

Professor Haseeb


2 Answers

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

Just use a JSON library like 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

like image 131
sloth Avatar answered Jan 17 '23 20:01

sloth


@Professor Haseeb Maybe you forget to Add the following to @Dominic Kexel solution:

Imports Newtonsoft.Json

Or use:

Dim result = Newtonsoft.Json.JsonConvert.DeserializeObject(x)
like image 38
Deja Vu Avatar answered Jan 17 '23 20:01

Deja Vu