Solved

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

Posted on 2011-02-11
6
3,160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

624 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