Solved

Parse Json String

Posted on 2013-11-06
8
709 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
Comment Utility
use .Net json parser from codeplex, works every time:
Json.NET 5.0
0
 
LVL 30

Expert Comment

by:Alexandre Simões
Comment Utility
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
Comment Utility
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
 
LVL 30

Expert Comment

by:Alexandre Simões
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:JoeBo747
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Excellent exactly what I wanted. Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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)

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now