Working with JSON
JSON (JavaScript Object Notation) is an exchange data format. It consists of a series of key-value pairs. It is very simple and is used as an alternative to XML.
JSON vs XML:
One of the advantages of JSON versus XML is its simplicity. In JavaScript we can use the function eval() to analyse a JSON text easily. However, ease of use is not relevant to choosing XML or JSON.
When to use JSON instead of XML:
We will usually use JSON in environments where the size of the data flow between the client and the server is very important and the data source is safe.
In App development we use JSON instead XML with REST protocol because it represents the data structure better and it is easier to code and process. It is perfect when we want to use it in a mobile phone app, where Internet connection is usually slow and internal storage is limited.
Limitations of JSON:
As we saw before we should not use JSON when the data source is not safe. Another instnace when we JSON should not be used is when XSLT process is required.
Example of using JSON:
We have seen when we can use JSON instead XM, for example in mobile apps. Next let’s see how we can use JSON in our app? For example, we can use JSON to get data from a database. If we have our database located on a host (this is very common, because mobiles have limited internal storage and we usually have our phones connected to Internet, so a database located on the web is not a bad idea) the best way for our app to communicate with the database is using JSON. Let’s see what we have to do to connect our app with a web database using JSON:
1. We have to create our database on a host.
2. With PHP scripts located in a host too, we do all the connection and the operations with the database.
1. Our app just has to send the information that the scripts need to query the database and retrieve the answer. The scripts will send the information to our app in JSON format, so we just need to use the mobile libraries for JSON.
How to store data in JSON:
We will see how to store data in JSON using PHP. Storing data with JSON using PHP is really easy, we just have to use the function json_encode to convert any type of data in a JSON string. Let’s see an example encoding an array.
$resultado[]=array(“logstatus”=>“1”);
json_encode($resultado);
Nesting data in JSON:
We can nest data with JSON and create more complex structures. Let’s see an example:
$myarray = array(“client” => array(“name” => “Jose”,surname =>”Tripiana”, age=>”23″));
$myjson = json_encode ($myarray);
How to load JSON data into a project:
In Android we can load data with JSON using just a method. The method StringRequest() request a JSON response. The data is sent by POST method and redefined with class Response.Listener. We have to redefine the method onResponse to get the JSON text.
String urlLocation = myURL;
StringRequest stringRequest = new StringRequest(Request.Method.POST, urlLocation,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray res = new JSONArray(response);
JSONObject object = res.getJSONObject(0);
logstatus = object.getString(“logstatus”);
//Process the information
} catch (JSONException e) {
e.printStackTrace();
}
}
}, …
We can send information to our script redefining the method Map. Putting the values in the Map Object we can send information by the POST method to our script and it can operate with this information.
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put(“data”, data);
return map;
}