Sunday, January 2, 2011

Quick JSON Tutorial

JSON.

In my first 2-3 year as 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 write also 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.

6 comments:

  1. excellent post...

    ReplyDelete
  2. Replacement together with counterfeit pieces range significantly fake breitling oneself together with you have to know it all. Any administrators for counterfeit structure pieces that have already have hardly any commonalities that will genuine pieces. Through approach to delivering for counterfeit pieces one can find mainly cheap counts implemented, the item can differ significantly rolex replica sale the very first pieces that can be really good. Any complete replications for Switzerland see may very well be said to while legitimate individuals. Successfully they're just very much the same as many as any smallest sections. It's possible you'll talk to certain abundant human beings and they'll inform you of the fact that quite possibly experiencing certain enormous amounts for revenue consumers regularly shop for pretend Breitling pieces to save some money. The grade of today’s patterns are sometimes made is not really lesser that will breitling replica sale Switzerland pieces when the genuine Switzerland clockworks, terrific superior substances together with sapphire wineglass are definitely the exact same that is to say veritable pieces. An individual's replacement will appearance similar to a legitimate veritable see together with it is impossible to inform you of to be basically replacement see. Immediately any replacement hublot replica thoroughly hinges on a superior. If you ever are interested Pretend Panerai pieces utilizing realistic Switzerland clockwork it will value a little bit more compared with replacement established utilizing Japanese people longines replica sale. Chances are you'll keep in mind that there's lots of many other essential elements of any see except for instrument.

    ReplyDelete