Solved

Parse Json String

Posted on 2013-11-06
8
720 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
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
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…

820 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