Solved

Visual Basic - Parsing JSON to Object and then to Standard Array

Posted on 2011-02-11
6
2,877 Views
Last Modified: 2012-05-11
Here is the code:
dim mydata() as String
Dim JsonSerializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim ReturnObject As Object = JsonSerializer.DeserializeObject(responseString)

Open in new window


Here is the JSON:
{
    "identification": {
        "items": [
            {
                "status": [
                    {
                        "name": "Inside",
                        "id": 831
                    }
                ],
                "housed": "upper",
                "name": "Checker-001",
                "actions": [
                    {
                        "name": "Check1",
                        "id": 1956,
                        "passed": false
                    },
                    {
                        "name": "Check2",
                        "id": 1957,
                        "passed": true
                    },
                    {
                        "name": "Check3",
                        "id": 1958,
                        "passed": true
                    },
                    {
                        "name": "Check4",
                        "id": 1959,
                        "passed": true
                    },
                    {
                        "name": "Check5",
                        "id": 1960,
                        "passed": true
                    }
                ],
                "id": 833
            },
            {
                "status": [
                    {
                        "name": "Inside",
                        "id": 832
                    }
                ],
                "housed": "upper",
                "name": "Checker-002",
                "actions": [
                    {
                        "name": "mycheck9",
                        "id": 1897,
                        "passed": false
                    },
                    {
                        "name": "mycheck10",
                        "id": 1898,
                        "passed": true
                    },
                    {
                        "name": "mycheck11",
                        "id": 1899,
                        "passed": true
                    },
                    {
                        "name": "mycheck12",
                        "id": 1900,
                        "passed": true
                    }
                ],
                "id": 819
            }
        ],
        "name": "my Identification",
        "id": "ZZ123-00A"
    }
}

Open in new window



How do I get the individal items in the Returned Object?

Above JSON has two items with different data, so how would I get:
Item, ID 833 - Action 2?

Then how would I get Item Id 819, say Action Name 3?

0
Comment
Question by:keith1001
6 Comments
 

Author Comment

by:keith1001
ID: 34874786
Ok I get the follow error:
Object reference not set to an instance of an object.

for this line of code:
mydata(1) = ReturnObject("inspection")("id")

But in the command window:
? ReturnObject("identification")("id")

It prints out the correct data.
0
 
LVL 10

Expert Comment

by:P1ST0LPETE
ID: 34875446
Hey bud.  I'm a C# developer and I don't have the time to convert this over to VB, however I'm basically doing the exact same thing in a program that I'm working on now.  The way I am doing it is I have specific classes designed to "house" my data.  In fact I load my data from the database into my classes, and then I create my JSON from the classes and send the JSON down to the page.  Then on post back, I parse the JSON back into the classes and go from there.

Anyway, going off the JSON that you have posted above I came up with the following class structure:
 
namespace YourWebApplication
{
    public class Identification
    {
        public int id = 0;
        public string name = "";
        public List<Item> items = new List<Item>();
    }

    public class Item
    {
        public int id = 0;
        public string name = "";
        public string housed = "";
        public List<State> status = new List<State>();
        public List<Action> actions = new List<Action>();
    }

    public class State
    {
        public int id = 0;
        public string name = "";
    }

    public class Action
    {
        public int id = 0;
        public string name = "";
        public bool passed = false;
    }
}

Open in new window


Now, with that class structure as part of your application, you can convert the JSON into a Identification class by doing the following, and then you can access the data properly by your defined class structure:
 
JavaScriptSerializer JsonSerializer = new JavaScriptSerializer();
Identification identification = JsonSerializer.Deserialize<Identification>(responseString);

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34876818
Easy enough to convert:

Namespace YourWebApplication
	Public Class Identification
		Public id As Integer = 0
		Public name As String = ""
		Public items As New List(Of Item)()
	End Class

	Public Class Item
		Public id As Integer = 0
		Public name As String = ""
		Public housed As String = ""
		Public status As New List(Of State)()
		Public actions As New List(Of Action)()
	End Class

	Public Class State
		Public id As Integer = 0
		Public name As String = ""
	End Class

	Public Class Action
		Public id As Integer = 0
		Public name As String = ""
		Public passed As Boolean = False
	End Class
End Namespace

Open in new window


Dim JsonSerializer As New JavaScriptSerializer()
Dim identification As Identification = JsonSerializer.Deserialize(Of Identification)(responseString)

Open in new window


0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 29

Expert Comment

by:Badotz
ID: 34878081
FYI: Visit http://www.json.org for all things JSON
0
 

Accepted Solution

by:
keith1001 earned 0 total points
ID: 34899893
This is what I needed and came up with:
 (ReturnObject("identification")("items")(i)("name"))

Open in new window

0
 

Author Closing Comment

by:keith1001
ID: 34936443
Figured out the correct way to get the specifics of the 'ReturnObject'
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…

821 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