Solved

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

Posted on 2011-02-11
6
2,782 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decoding Special HTML Characters using Javascript? 2 23
cannot connect to sqlserver 8 25
.NET 2008 VB and C# 6 27
Help with adding DLL file in Windows project 20 28
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

808 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