JSON

JSON stands for JavaScript Object Notation. It is defined on the site, http://www.json.org/. This is a textual notation in the same way as the object literals are defined in JavaScript . An object representation starts with the { character and ends with the } character. The text in between defines the fields of the objects in the form, string : value. The string is the name of the field, and since JSON wants to be language agnostic, it allows any characters to be a part of the name of a field, and thus this string (as well as any string in JSON) should start and end with the " characters.

This may seem strange and, many times, when you start working with JSON, it is easy to forget and write { myObject : "has a string" } instead of the correct { "myObject" : "has a string" } notation.

Commas separate the fields. You can also have arrays in JSON. They start and end with [ and ] characters, respectively, and they contain comma-separated values. The value in an object field or in an array can be a string, a number, an object, an array, or one of the constants, true, false, and null.

Generally speaking, JSON is a very simple notation to describe data that can be stored in an object. It is easy to write using text editors and easy to read, and thus it is easier to debug any communication that uses JSON instead of more complex formats. Ways to convert JSON to a Java object and the other way round, are readily available in libraries that we will use in this chapter. A sample JSON object that describes a product from our sample code is also available in the source code of the program, as follows:

{"id":"125","title":"Bar Stool","description":"another furniture","size":[20.0,2.0,18.0],"weight":300.0}

Note that the formatting of JSON does not require a new line, but at the same time, this is also possible. Program-generated JSON objects are usually compact and are not formatted. When we edit some object using a text editor, we tend to format the indentation of the fields in the same way as we usually do in Java programming.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.134.133