Solved

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

Posted on 2011-02-11
6
2,636 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 29

Expert Comment

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

Accepted Solution

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

Open in new window

0
 

Author Closing Comment

by:keith1001
Comment Utility
Figured out the correct way to get the specifics of the 'ReturnObject'
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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now