How to Parse Json in Android Application

JSON stands for JavaScript Object Notation.It is an independent data trade format and is the perfect various for XML. This chapter explains the way to parse the JSON file and extract essential info from it.

Don't Miss

Android supplies 4 completely different classes to govern JSON data. These classes are JSONArray,JSONObject,JSONStringer and JSONTokenizer.

The first step is to determine the fields within the JSON data through which you have an interest in. For example. In the JSON given below:

{
   "info":
   {
      "Name":"Abhay Anand",
      "Job Role": "Software Developer",
      "Website":"www.studytutorial.in"
   },
   "Country":[
      {
         "Country": "India",
         "State":"New Delhi",
         "City":"Delhi"
      }
   ],
	
  "blogs":
   {
      "News Blogs":"www.wnewsreport.com",
      "Developer Blogs": "www.studytutorial.in",
   }
}

JSON Component

An JSON file include many elements. Here is the table defining the components of an JSON file and their description:

Sr.NoComponent & description
1Array[] In a JSON file , square bracket [] represents a JSON array
2Objects{} In a JSON file, curly bracket {} represents a JSON object
3Key A JSON object contains a key that is just a string. Pairs of key/value make up a JSON object
4Value Each key has a value that could be string , integer or double e.t.c

Json Parsing

For parsing a JSON object, we are going to create an object of class JSONObject and specify a string containing JSON data to it. Its syntax is:

String jsonString;
JSONObject reader = new JSONObject(jsonString);

The final step is to parse the JSON. A JSON file consist of various object with totally different key/value pair e.t.c. So JSONObject has a separate function for parsing every of the component of JSON file. Its syntax is given below

JSONObject info  = reader.getJSONObject("info");
name = info.getString("Name");
jobRole = info.getString("job Role");
			
JSONObject blogs  = reader.getJSONObject("blogs");
newsBlogs = main.getString("News Blogs");

The method getJSONObject returns the JSON object. The method getString returns the string value of the required key.

Apart from the these methods , there are different methods offered by this class for higher parsing JSON information. These methods are listed below

Sr.NoMethod & description
1get(String name) – This method simply Returns the value however within the type of Object type
2getBoolean(String name) – This method returns the boolean value specified by the key
3getDouble(String name) – This method returns the double value specified by the key
4getInt(String name) – This method returns the integer value specified by the key
5getLong(String name) – This method returns the long value specified by the key
6length() – This method returns the number of name/value mappings in this object..
7names() – This method returns an array containing the string names in this object.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

564FansLike

Recent Posts

Concept of Session in Laravel Tutorial

Sessions are used to store details about the user throughout the requests. Laravel supplies various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used as a result of it's light-weight....

Laravel Url Generation Tutorial

Our web application revolves around routes and URLs. After all, they're what direct our users to our pages. At the end of the day, serving pages is what any web application should do. Our users may...

Concept of Laravel Views Tutorial

In MVC framework, the letter "V" stands for Views. It separates the application logic and presentation logic. Views are saved in resources/views listing. Generally, the view contains the HTML which might be served by the application.

Related Articles

Concept of Session in Laravel Tutorial

Sessions are used to store details about the user throughout the requests. Laravel supplies various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used as a result of it's light-weight....

Laravel Url Generation Tutorial

Our web application revolves around routes and URLs. After all, they're what direct our users to our pages. At the end of the day, serving pages is what any web application should do. Our users may...

Concept of Laravel Views Tutorial

In MVC framework, the letter "V" stands for Views. It separates the application logic and presentation logic. Views are saved in resources/views listing. Generally, the view contains the HTML which might be served by the application.
WP2Social Auto Publish Powered By : XYZScripts.com