Quick JSON Tutorial
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
Post Comment
I have to express thanks to you for rescuing me from such a difficulty. Because of checking through the world wide web and coming across thoughts that were not helpful, I was thinking my entire life was over. Existing without the presence of solutions to the difficulties you've fixed through your guide is a critical case, and those which might have negatively affected my entire career if I had not come across the blog. That competence and kindness in playing with the whole thing was helpful. I'm not sure what I would have done if I had not discovered such a step like this. I'm able to at this time relish my future. Thanks very much for your high quality and result oriented guide. I won't think twice to recommend your web page to anyone who wants and needs counselling about this subject matter.
I precisely desired to thank you so much once more. I do not know the things I could possibly have made to happen without those ideas provided by you relating to such a area. It was before a very daunting condition for me, nevertheless observing the very professional strategy you dealt with that made me to cry for happiness. Now i'm thankful for the information and then hope that you find out what a powerful job your are getting into training people today via your blog. Most probably you haven't met any of us.
Thank you for all your valuable hard work on this web page. Kim delights in setting aside time for investigations and it is obvious why. A number of us know all relating to the compelling medium you present helpful tips and hints on the website and as well cause participation from some others on this situation and our own daughter has always been discovering a whole lot. Take advantage of the remaining portion of the year. You're doing a pretty cool job.
I not to mention my guys came studying the excellent strategies from your web page then unexpectedly I had an awful suspicion I had not thanked the site owner for those tips. My ladies came for this reason warmed to read all of them and already have definitely been taking pleasure in them. Thank you for being well helpful and then for finding this kind of exceptional resources millions of individuals are really desirous to know about. Our honest regret for not expressing appreciation to sooner.
My husband and i got so satisfied Albert could carry out his homework out of the ideas he received through your blog. It's not at all simplistic just to find yourself giving for free secrets and techniques that other people might have been making money from. We fully understand we have got the website owner to appreciate for that. The most important illustrations you've made, the easy web site menu, the friendships you will make it possible to create - it's got many exceptional, and it is aiding our son in addition to us do think that situation is amusing, and that is especially important. Thank you for the whole thing!
1fUMqp Looking forward to reading more. Great blog article.Really looking forward to read more. Really Cool.