Parse JSON data android example, JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML.
In this example we will learn how to parse JSON in android.

For better understanding taking a simple and static JSON to parse.

Sample JSON:

 {
    "Android": [
        {
            "p_name": "Elite Applocker",
            "p_id": "1",
            "author_name": "Appsinfoway"
        },
        {
            "p_name": "AI Applocker",
            "p_id": "2",
            "author_name": "Appsinfoway"
        }
    ]
} 

JSonParser.java

 
public class JSonParser extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parse_json);
         
        final TextView output = (TextView) findViewById(R.id.output);
        final Button bparsejson      = (Button) findViewById(R.id.bparsejson);
         
        /************  Static JSON data ***********/
        final String strJson = "{ 
           "Android" :
                    [
                       {
                          "p_name":"Elite Applocker",
                          "p_id":"1",
                          "author_name":"Appsinfoway"
                       }, 
                       {
                          "p_name":"AI Applocker",
                          "p_id":"2",
                          "author_name":"Appsinfoway"
                       }
                     ] 
        }";
         
        String dataToBeParsed = "Click on button to parse JSON.\n\n JSON DATA : \n\n"+strJson;
        output.setText(dataToBeParsed);
         
        /******** Listener for button click ********/
        bparsejson.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
         
               String OutputData = "";
               JSONObject jsonResponse;
                     
               try {
                     
                    /****** Creates a new JSONObject with name/value mappings from the JSON string. ********/
                    jsonResponse = new JSONObject(strJson);
                     
                    /***** Returns the value mapped by name if it exists and is a JSONArray. ***/
                    /*******  Returns null otherwise.  *******/
                    JSONArray jsonMainNode = jsonResponse.optJSONArray("Android");
                     
                    /*********** Process each JSON Node ************/
 
                    int lengthJsonArr = jsonMainNode.length();  
 
                    for(int i=0; i < lengthJsonArr; i++) 
                    {
                        /****** Get Object for each JSON node.***********/
                        JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                         
                        /******* Fetch node values **********/
                        int p_id        = Integer.parseInt(jsonChildNode.optString("p_id").toString());
                        String p_name   = jsonChildNode.optString("p_name").toString();
                        String author_name = jsonChildNode.optString("author_name").toString();
                         
                         
                        OutputData += "Node : \n\n     "+ product_id +" | "
                                                        + product_name +" | "
                                                        + author_name +" \n\n ";
                        //Log.i("JSON parse", product_name );
                   }
                     
                    /************ Show Output on screen/activity **********/
 
                    output.setText( OutputData );
                     
                } catch (JSONException e) {
         
                    e.printStackTrace();
                }
 
            }
        });
    }
}

activity_parse_json.xml