Introducing JSON
page 4 of 9
by Bilal Haidar
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 47866/ 64

JSON Data Types

JSON is mainly based on two structures:

A collection of name/value pairs which can be thought of as an object, structure, hash table, keyed list, and dictionary in most of the Object Oriented Languages.

A list of values which is similar to the array in most Object Oriented Programming Languages.

JSON Object

An object is represented as follows:

Listing 2: JSON Object

{
  // Properties (name/value pairs separated by comma)
}

When you want to construct a JSON object you simply start by opening right opening curly braces then write down your name/value pairs which are thought of as properties in most of OOP languages.

The object can contain simple name/value pairs, an array of values, an array of objects, and simple values.

Once you have finished writing your own properties, you can add left opening curly braces as if you are closing the object. For those of you who have an experience with C# or JAVA, in those two languages we use the left and right opening curly braces to specify the beginning and ending of an object.

To access a property within your JSON object in JavaScript, you simply write the following:

Listing 3: JSON Object Access

var fname = jsonObject.FirstName

To be able to access a JSON object this way as shown in Listing 3 above, you will have to first parse the string JSON object generated by a server-side language into a JSON object. You can simply use the eval function in JavaScript; however, this function has many security concerns because it can execute any JavaScript statement. A better and clearer way of parsing your string JSON objects to real JSON objects is to use a library that is distributed for free from www.json.org. I will look at this library later on in this article.

JSON Array

A JSON array is a simple array structure that I am sure you have extensively used in your applications while developing either windows or web applications.

In JSON an array can hold simple values or objects. An example of a JSON array is as follows:

Listing 4: JSON Simple Array

"simpleArray": ["value 1""value 2""value 3"]

As you can see, I have constructed a simple array of strings named simplearray. You can even construct an array of integers, floats, double, and even Boolean values. The reason behind naming the array is to be able access its elements. This will be clearer in the next listing where I show you how to access a complex array of objects.

You can also have a more complicated array where you might embed an array of objects.

Listing 5: JSON Complex

"arrayOfObjects":
[
      {
            "name1": "value1",
            "name2": "value2"
      },
      {
            "name3": "value3",
            "name4": "value4"
      }
]

As shown in the above listing, each element is simply a JSON object which is normally allowed in JSON. To access the first property of the second object in the above array, you might write something similar to this code:

Listing 6: JSON Complex Array Access

var name1 = arrayOfObjects[1].name1

Again, if you are going to use the above code you will have to parse the JSON string array into a real JSON array. More on this comes in the next sections of this article.

JSON Property

As mentioned above, JSON is based upon the object and array data structures. Within these data structures you would definitely use properties and values.

A value can be a string, number, object, array, true, false, or null values.

A property is nothing but a value with a name. You can think of each name/value pair as a property.

To have an extensive explanation on the JSON data types please refer to the official JSON website which can be reached at the following URL.


View Entire Article

User Comments

Title: www   
Name: ww
Date: 2012-12-17 2:41:58 AM
Comment:
www
Title: Argument for Gun Control   
Name: Rick
Date: 2012-12-14 10:19:16 PM
Comment:
Time to take the guns away from the crazies
Title: demo   
Name: vishal
Date: 2012-11-30 7:20:06 AM
Comment:
i am new user....
Title: jason verification   
Name: madhan
Date: 2012-09-13 3:14:23 AM
Comment:
I need to check the jason
Title: fsgfds   
Name: sdfg
Date: 2012-08-15 3:06:30 PM
Comment:
fsdg
Title: dsavsd   
Name: vsdv
Date: 2012-07-10 7:14:56 AM
Comment:
lknvlksndv
Title: m,.,   
Name: ,m.,m.
Date: 2012-06-13 12:30:17 AM
Comment:
,m.,m.
Title: Re: Thanks   
Name: Bilal Haidar
Date: 2012-06-12 9:03:50 AM
Comment:
Most welcome Salman :)
Title: Thanks   
Name: Salman ansari
Date: 2012-06-12 8:59:06 AM
Comment:
Thankyou very much......very useful to me on learning stage of JSON
Title: answer to the query from DHEERAJ   
Name: isha
Date: 2011-05-06 2:05:06 AM
Comment:
cut and paste the 2 lines of code ie.
var postRequest = new HttpRequest();
postRequest.failureCallback = requestFailed; inside the function getemployeedetails on the default.js page in the beginning. Apllication runs fine then. thank you
Title: answer to the query from DHEERAJ   
Name: isha
Date: 2011-05-06 2:03:11 AM
Comment:
the query that dheeraj has asked, i faced the same. it is not the issue of IE7. if u check the Default.js page, there u will find 2 lines of code ie.
var postRequest = new HttpRequest();
postRequest.failureCallback = requestFailed;
written above the function getEmployeedetails().
please cut and paste these 2 lines inside this function in the beginning. the application runs fine then. thank you for this amazing application for beginners in JSON/JQUERY
Title: Best Tutorial   
Name: Yogesh Nandre
Date: 2011-01-20 2:35:00 AM
Comment:
Best due to very easy to learn for new one...
Title: Thank You   
Name: Suresh
Date: 2010-12-06 6:26:19 AM
Comment:
Very nice stuff
Title: Json   
Name: Wilmer Ferreira
Date: 2010-09-06 4:16:52 PM
Comment:
In Visual Studio 2010 exits the System.Runtime.Serialization.Json namespace
Title: JSON   
Name: Reshma
Date: 2010-09-06 3:52:38 AM
Comment:
This article gives me very useful and functionality information about json..
Title: JSON   
Name: Vikas
Date: 2010-05-19 12:22:22 AM
Comment:
This article provides me with the great help on what i needed the most
Title: JSON   
Name: Leela
Date: 2010-03-16 12:47:50 AM
Comment:
This article is very useful.
I am a beginner in .NET and i find many useful things ..)
Thankyou
Title: QUERY   
Name: DHIRAJ
Date: 2009-11-30 1:24:26 AM
Comment:
THERE IS SOME ERROR IN THIS ARTICLE. IF WE SELECT ANY NAME FROM DROPDOWNLIST IT DISPLAYS THE RECORD BUT IF WE SELECT ANOTHER NAME FROM DROPDOWNLIST IT DOES NOT DISPLAYS ANY RECORD.

Plz let me know on my email id:
dhiraj.pandey@nagsoft.com,dhirajkumarpandey@ymail.com
Waiting for ur reply on urgent basis....
Title: Re: IE7 issue   
Name: Martin
Date: 2008-04-04 2:10:49 AM
Comment:
Ok, but what can I do to make it work in IE7 ? I tryed to set postRequest = null; in the GetEmployeeDetails() method but then it turns back with an error ?
Title: Re:   
Name: Bilal Haidar [MVP]
Date: 2008-04-04 2:04:17 AM
Comment:
This is not a problem in the code! I once asked about that and they said it is an IE issue!! Didn't check later what they have done to solve it!

Thanks
Title: IE7 issue   
Name: Martin
Date: 2008-04-04 1:42:19 AM
Comment:
Hi,

I just downloaded the demo app and tested it in IE7 and when i select an "employee" from the dropdown list it shows up as expected, but when I try to choose another one from the dropdown nothing happens, is there an bug in the code ?
Title: Re:   
Name: Bilal Haidar [MVP]
Date: 2007-05-19 2:41:45 AM
Comment:
Yes it is :)

Regards
Title: Where is Jason?   
Name: AbsCoder
Date: 2007-05-18 6:16:26 PM
Comment:
Thanks for the article; good stuff. In the article you make mention twice of "great JSON DLL that is called Jason.NET". Perhaps, I'm dense, but I can't seem to locate said DLL. Do you mean Newtonsoft's Json.NET library? Again, thanks for your contribution to the community!






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-07-13 12:12:21 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search