Download Code

Android JSON Parser Tutorial

In android json parser tutorial you will learn about JSON(JavaScript Object Notation) basic.How to parse data using JSON Parser in android application.You can learn more from w3Schools.

JSON

  • Stands for JavaScript Object Notation.
  • for storing and exchanging data.
  • lightweight independent language.
  • easier to use alternative of XML.

Syntax

  • Data in name/value pairs.
  • Data is separated by commas.
  • Curly braces hold objects.
  • Square brackets hold arrays.

JSON,JSON in android,JSON in android application, what is JSON,JSON Syntax,how to use JSON in android, JSONObject,JSONObject in android,JSONObject in androd application, JSONArray,JSONArray in android,JSONArray in android application, how to use JSONObject in android,how to use JSONObject in android application, how to use JSONArray in android,How to use JSONArray in android application, Name/value pairs in android,JSON Parser,JSON parser in android, JSON parser in android application,how to parse data using json in android,how to use JSON parser in android application,TextView in android application, how to display data from JSON parser in textview,android json,android json parser,android json parser tutorial,json parser tutorial,

JSON Parser

JSON Parser is basically used to convert machine language to that language which can understood by app.

Create new android json parser tutorial application project.

Go to activity_main.xml and add a TextView.

android json,android json parser,android json parser tutorial,json parser tutorial,

Next MainActivity.java class.Do the following steps.

  • Create variable of TextView and refer it’s id.
  • Create variable to store extracted string.
  • Create variable and store JSONstring in it.
  • Under exception handling getParentJSONObject.
  • Then getJSONArray.
  • Store in a string extracted string.
  • Set to TextView.

MainActivity.java

package developer.codecjson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends Activity {
	TextView output;   // textview variable 
	String data = "";  // variable to display data

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initialize();   // refering id method
		//JSONString from which data extract
		String strJson="{'Android Codec' :["
				+ "{'id':'01','name':'Expandable ListView','words':'500'},"
				+ "{'id':'02','name':'JSON Data','words':'1022'},"
				+ "{'id':'03', 'name':'Spinner','words':'510'}"
				+ "] "
				+ "}";

		try {
			//getting parent object from JSONString
			JSONObject  parentObject = new JSONObject(strJson);

			//Get the instance of JSONArray that contains JSONObjects
			JSONArray jsonArray = parentObject.optJSONArray("Android Codec");

			//displaying JSONArray data in textView
			for(int i=0; i < jsonArray.length(); i++){ JSONObject jsonObject = jsonArray.getJSONObject(i); // getting JSON Object at I'th index //getting string of "name" => id and parse into integer
				int id = Integer.parseInt(jsonObject.optString("id").toString());
				//getting string of "name" => name
				String name = jsonObject.optString("name").toString();
				// getting string and parse into float
				float total_words = Float.parseFloat(jsonObject.optString("words").toString());
				// storing parsed data into string "data"
				data += "\n id= "+ id +" \n Name= "+ name +" \n Words= "+ total_words +" \n ";
			}
			// setting data string to textview output
			output.setText(data);
		} catch (JSONException e) {e.printStackTrace();}
	}
	public void initialize(){
		output = (TextView) findViewById(R.id.textView1);
	}
}

Run your android json parser tutorial application project.

android json,android json parser,android json parser tutorial,json parser tutorial,

That’s it for this tutorial.
Thanks.