package com.androidhive.jsonparsing;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
Json have only three data items which we use to retrieve data:
1. Json array
"contacts": [
{
"id": "c200",
"name": "Lucky Rana",
"email": "luckyrana@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}]
To retrieve this "contact" array we use following syntex:
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
contacts = json.getJSONArray("contact");
Log.d("Contact array Data",json.toString());
}
catch (JSONException e)
{
e.printStackTrace();
}
2. Json object
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
To retrieve this "phone" object data we use following syntex:
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
contacts = json.getJSONArray("contact");
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
JSONObject phone = c.getJSONObject(phone);
Log.d("Phone object Data",phone.toString());
}
catch (JSONException e)
{
e.printStackTrace();
}
3. Json string
"id": "c200"
To retrieve this "phone" object data we use following syntex:
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
try {
contacts = json.getJSONArray("contact");
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
// Phone number is agin JSON Object
JSONObject phone = c.getJSONObject(phone);
String mobile = phone.getString("mobile");
String home = phone.getString("home");
String office = phone.getString("office");
Log.d("String Data",mobile.toString());
}
catch (JSONException e)
{
e.printStackTrace();
}

Example 1: