4

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

2 Answers 2

9

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

Sign up to request clarification or add additional context in comments.

3 Comments

i cant get it to work... :p error i get is JsonConvert not declared
Have you actually added a reference to Json.NET to your project and imported the corrct namespace?
how should it be done without table name? what if i only have this [{'title':'HarryPotter','pages':'134'}]
3

@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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.