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

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"