Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

JSON: What it is and how to work with it

In this article we will see some of the characteristics and properties of the JSON, and analyze the differences to other data formats and how to use basic client side and server side.

Introduction

As the XML returned by the server in asynchronous applications also have the JSON data format is another. The JSON format was originally created by Douglas Crockford and means Javascript Object Notation. Whenever we want to send information between a web page and a server will need some way to format them as plain text, XML or even JSON is more than a way to send and retrieve data.

JSON is used in large portals like Google and Yahoo. JSON is also well suited for front-end developers who are not used to read or write XML. JSON is much simpler to handle because it is similar to arrays and lists which is quite common in virtually all languages ​​including Javascript.

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

Many say that the data format JSON is much higher than the data in XML format especially when it comes to Javascript and asynchronous applications. Few people know JSON, though many know that XML is a standard recognized and fairly widespread, however XML is big and sometimes difficult to deal with, otherwise the XML can handle just about anything and diverse data types. When we speak of the speed JSON is faster than XML, but even then the XML is also fast, one does not cancel the other but JSON is statistically faster.

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.

Despite the various options XML JSON cares basically pass information from one Web page to a server and then do the reverse trajectory without much effort, extra tools or traverse a DOM tree. Therefore, to work directly in Javascript so lightweight and easy to use JSON. However, server-side development environments such as Java, PHP, Perl, C #, etc need to support JSON, today most languages ​​already provides this support through specific APIs.

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.

{“endereco”: [
   {
      “rua”: Leonor Viana,
      “cidade”: São Paulo,
      “estado”: SP
   }
]};

As with the XML data format JSON is also quite intuitive. To work with JSON Javascript common use not requiring any special object model.

To retrieve the JSON data returned by the server could use the Javascript code below:

Listing 2: Example of Java script code manipulating JSON data.

var jsonData = eval(‘(‘ + request.responseText + ‘)’ );
var rua = jsonData.endereco[0].rua;
var cidade = jsonData.endereco[0].cidade;
var estado = jsonData.endereco[0].estado;

See that endereco[0] 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.

In the code above we use the responseText property to receive data from the server in plain text format, after it converted to a JavaScript object, so we use the eval function above.

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.

Conclusions

JSON is a data format such as XML, but unlike XML, JSON using the developer does not need the DOM to work with JSON data, it has its own way through lists and arrays. JSON is nothing more than pure Javascript and can send data in JSON format to the server in our requests, and we can send XML or plain text, but the work is done in a much more elegant and simple JSON. On the server side if we need to manipulate data using a JSON library to capture incoming data and converts them into arrays or some format that he can use. When choosing the data format they wish to use should evaluate which is best for your needs, typically uses XML or JSON each having its advantages and disadvantages as discussed above.



Higor Medeiros (higorowen@gmail.com) is a student at the University of Vale do Rio dos Sinos in Computer Science studying in 4th semester, where she also works in scientific research in the area of ​​artificial intelli...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login