Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Quick JSON Tutorial

, 14 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A very quick and usable tutorial for JSON

JSON...

In my first 2-3 years as a developer, I defined myself as a "Control Developer". The definition for Control Developer is a very simple one. A developer who believes that anything and everything can be created using Ctrl + C and Ctrl + V (meaning copy and paste).

This approach was working great as long as I didn't need to create brand new solutions for brand new features with brand new problems.

As I was saying,

About 15 months ago when I was working in my old company (FixYa) I was asked by my team leader to pass a JSON object to the server and to expect to get a different JSON object back.

As a young proud developer, the first thing I said was - “sure. No problem". And then the problem started.

I couldn't find any good quick tutorial for JSON. What I needed was how to write a JSON object easily, not a full explanation of the JSON parser.

Now, 15 months later, I'm proud to present what I was looking for then - a very quick and usable tutorial for JSON:

JSON - JavaScript Object Notation

The data types a JSON object can contain are:

  • Number
  • String
  • Boolean
  • Array
  • Another JSON Object
  • Functions
  • Null

Here I'm about to slowly fill a JSON object called: User.

var User = {};

This is the definition of User object. I can also write this:

User.Age = 15;
User.Name = "Igor";
User.IsUnderAge = true;

Of course, a better way would be:

var User = {
                Age: 15,
                Name: "Igor",
                IsUnderAge: true
};

As I mentioned earlier, you can also insert arrays inside the JSON object:

var User = {
                Friends: [
                                "Sergey",
                                "Uri",
                                "Noam"
                ]
};

If you wanted to make things more complicated, you would make each friend as a User object:

var User = {
                Name: "Igor",
                Age: 15,
                Friends: [
                                {Name: "Sergey", Age: 15},
                                {Name: "Uri", Age: 15},
                                {Name: "Noam", Age: 15}
                ],
                IsUnderAge: function() {
                                return this.Age > 21 ? true : false;
                }
};

Now you have Igor as a very detailed and easy to read user object.

In order to understand how to make "User" a generic system object, read "JSON Midsection".

You can also view my post called "JSON Object Code Example" for a clean code example.

Good luck.

Elad,
Lead Developer at Sports Betting Tech

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

DevAffair
Team Leader Sports Betting Tech
Israel Israel
No Biography provided
Follow on   Twitter

Comments and Discussions

 
QuestionQuick JSON Tutorial Pinmemberjainramesh6-Sep-11 20:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 14 Jun 2011
Article Copyright 2011 by DevAffair
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid