Parse Json String

I  have the following Json which I wish to loop through in vb.net and extract the questions and results. I have attempted to use Newtonsoft.json but I get errors although JSONlint validates the Json. What is the correct method to achieve this?


{
    "Vehicle_Check": [
        19,
        {
            "question": "Brakes",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Water Levels",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Horn",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Washers",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Wipers",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Indicators",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Reflectors",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Oil Levels",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Lights",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Mirrors",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Steering",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Tyres Wheels",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Battery",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Fuel or Oil Leaks",
            "result": "OK",
            "Fault": ""
        },
        {
            "question": "Other",
            "result": "OK",
            "Fault": ""
        }
    ]
}

Dim o As JObject = JObject.Parse(VCItem.Check)
Dim results As List(Of JToken) = o.Children().ToList
For Each item As JProperty In results
    item.CreateReader()
    Select Case item.Name
        'process data here
    End Select
Next
This returns the whole string,  can you point me in the right direction.

Regards,
Joe
LVL 1
JoeBo747Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Alexandre SimõesConnect With a Mentor Manager / Technology SpecialistCommented:
Basically you need to create a class with the same public properties each json item has: Question, Result and Fault.

Then deserialze using something like:
var jarray = JsonConvert.DeserializeObject<List<MyItem>>(myjsondata);

Open in new window


myjsondata is your json string.

Remember to take the 19 out.
0
 
Meir RivkinFull stack Software EngineerCommented:
use .Net json parser from codeplex, works every time:
Json.NET 5.0
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
If you want to use the .net framework built-in class for this look here:
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

Json.net is faster and overall better, but javascriptserializer will also do the trick.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
JoeBo747Author Commented:
Hi Sedgwick,

My code above uses json.net but does not return the expected results.  Do you have an example either c# or vb that pases the supplied json down into individual elements.

Regards,
Joe
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
What I see is that your json is not valid as a .net object.

Vehicle_Check is mostly an array of objects but actually the first collection item is only a number. I believe its purpose is to tell the amount of records... can you just remove it from there?
If you remove that 19, from your json I believe everything will work as expected.
0
 
JoeBo747Author Commented:
Hi AlexCode,

Yes I am able to remove the 19, from the json but I am still unable to parse the contents using the json.net library the code returns the whole json string.  Are you able to suggest an alternative that will achieve the goal of of separating each of the elements.

Regards,
Joe
0
 
JoeBo747Author Commented:
Hi,

Thanks for your reply I will attempt the fix and get back to you.

Regards,
Joe
0
 
JoeBo747Author Commented:
Excellent exactly what I wanted. Thanks
0
All Courses

From novice to tech pro — start learning today.