Solved

Parse Json String

Posted on 2013-11-06
8
733 Views
Last Modified: 2013-11-13
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
0
Comment
Question by:JoeBo747
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39627088
use .Net json parser from codeplex, works every time:
Json.NET 5.0
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39627103
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
 
LVL 1

Author Comment

by:JoeBo747
ID: 39627200
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
Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39627374
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
 
LVL 1

Author Comment

by:JoeBo747
ID: 39628234
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
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 39628785
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
 
LVL 1

Author Comment

by:JoeBo747
ID: 39629814
Hi,

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

Regards,
Joe
0
 
LVL 1

Author Closing Comment

by:JoeBo747
ID: 39644952
Excellent exactly what I wanted. Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question