All developers are wondering which data format to use: plain text, XML or JSON. Plain text is not suited because you need to define a custom format that you only need to understand and manipulate them in a primitive way, XML is an excellent option being used on more complex data structures and JSON is fairly stated in various situations including those in we work with rows of a table or matrix.
JSON versus XML
Another point is that the much discussed JSON is easy to work with and manipulate enough to know arrays, on the other hand XML syntax has a higher and defined may be transformed to XSLT and handled in web services.
As you can see there are advantages and disadvantages to working with one of the approaches, it behooves you to analyze each one and see which weighs more to use in your application. Below we'll talk a bit more about JSON.
Client-side using JSON
JSON uses keys rather than signs of greater and lesser used in XML, but can store the same types of data that the XML. Below is an example of the JSON data:
Listing 1: Sample data in JSON.
“rua”: Leonor Viana,
“cidade”: São Paulo,
Listing 2: Example of Java script code manipulating JSON data.
var jsonData = eval(‘(‘ + request.responseText + ‘)’ );
var rua = jsonData.endereco.rua;
var cidade = jsonData.endereco.cidade;
var estado = jsonData.endereco.estado;
See that endereco returns the first item and only address if we had more items would define another index.
Using JSON note that we need not worry about manipulating DOM trees or any other tool support, JSON handles everything.
Using JSON on the Server
To create and display JSON data on server side we need a library so we can manipulate it. JSON libraries are very helpful in handling the JSON because they know exactly how to handle this information. Among the languages that support JSON have C / C + +, C #, ColdFusion, ActionScript, Java, Perl, PHP, ASP 3.0, Python, Ruby.
In PHP has a library JSON.php which is widely used, while for Java is a library JSON4J quite popular among Java developers.
The libraries make it very easy to use JSON on the server side and know how to manipulate JSON language specifies a developer should study how the chosen library handles the JSON data and what types of operations it provides.