Published on Feb 22,2017
694 Views
Email Post


What is JSON?

JSON is an acronym for JavaScript Object Notation and is a way to store information in an organized, easy-to-access manner. In simpler terms, it gives a readable collection of data that one can access in a logical manner. The Javascript object notation is a data exchange format, which comes into picture while the data travels between web servers and mobile with the use of JSON objects. JSON objects are based on key/value pairs. In other words, when the information is read back , the key is read. The value, however, can be a number, string or Boolean number.

JSON SYNTAX:

To get a better understanding of JSON Syntax, the following 4 points must be taken into consideration:

a)      The Data is in name/value pairs.

b)      The Data is separated by commas.

c)       There is only one object held  in curly braces.

d)      There are multiple objects in Square brackets (An array of multiple objects).

Here is an example of a single object in curly braces:

{“name”:”Edureka”, “course”:”Android”}

Here is an example of multiple objects in square & curly brackets:

{“company”:

[

{“name” : “Edureka”, “course”: “Android”}

{“name”:”Edureka”, “course”: “iOS”}

]

}

As we move on, we notice a static URL in the main activity. In this case, the values from JSON are:

a) Vehicletype

b) Vehiclecolor

c) Fuel

d) Treadtype

The values are going to be read back from the object that one gets from JSON. The ArrayList of hashmap, which contains a string is also called a JSON list.

Its important to note that JSON comes into picture every time data gets downloaded from server to client.

The next step is to create a listview object that populates from the data sets and is downloaded using JSON object. The second activity is the creation of progress task that extends Async Task.

Three parameters to be taken into consideration:

1)      String – the url to be passed, has to be of type string

2)      Void – not using object at all

3)      Boolean- will notify if download is done or not.

A dialogue box is also created on the basis of the activity context.

Along with that, in the pre-execute stage, a message to dialogue box saying ‘show the dialogue box’ is set.

DoIN Background

The first step in the DoIN background is creation of JSON parser object. In simpler terms, a class is created, which parses the response.

As we get back to the main activity, we get the JSON object ArrayList from class object.getjsonfromurl.

Here, the url is parsed to get the JSON ArrayList.

The parsing of url to this method creates a string builder object, default http client, http get, quest response & status line. The status line here gives a score, which denotes whether the url or web server is up or down. In this case, the score is 200.

Subsequently, the response entity is received and converted to an InputStream, from which a BufferedReader is created. We get the reader here by reading line by line and appending it. Once everything is appended to the string builder, it is converted into string, and an array of JSON objects is created.

The next step involves creating new JSON array, and passing string to it, which will return the array list of JSON objects. Then, we return back to JSON array.

As we progress, there comes a need to iterate object list through JSON and push information onto the list. Iteration is done until there is an object on the JSON ArrayList.

We then create an object from getting JSON object at index. After getting the complete object, we get the values, such as vehicle type, colour, fuel, tread and other keynames.

The keyname is agreed upon in the beginning, based on the data exchange format.

Once the values from various objects are taken, a hashmap is created and all the values are then put in the very hashmap inside its index. We also add hashmap single objects to the JSON list of array hashmap, which will take JSON list as an argument list and add to the one, where the data needs to be showed.

In this case, we have 4 textviews to show the data. The data has to be filled up by vehicle type, colour, track, tread. The integer and text view are replaced with the values inside the arrays and the adapter is set to list view.

Got a question for us? Mention it in the comments section and we will get back to you. 

Related Posts:

Android Tutorial for Beginners Part-1 : Activity Component

Android Development course

About Author
edureka
Published on Feb 22,2017

Share on

Browse Categories

Comments
0 Comments