Solved

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

Posted on 2011-02-11
6
2,737 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with LINQ and XML 10 28
Change javascript css 1 21
insert text field data into html script 13 31
VB.NET 2008 (3.5 Framework) Remove all items from List 3 4
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

863 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

22 Experts available now in Live!

Get 1:1 Help Now