Possible Duplicate:
How can I parse this JSON in Android?
I am following this link to parse my json but i am unable to perform as i am confused . and even the duplicate link does not provide any reference, those users who voted to close this question can answer my question when their provided LINK does not provide any help to solve these question?
i think those who can not answer the question have no rights to edit some one's question nor have rights to close the question when their provided link does not provide any point to solve this. Help me to parse data.
[
{
"id": "c200",
"name": "XYZ",
"email": "xyz@gmail.com",
},
{
"id": "c201",
"name": "Johnny",
"email": "john_johnny@gmail.com",
}]
[ANSWER]
Since it is an closed post so i am posting my answer in question:
//JSONArray
JSONArray hospital = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
String json = jParser.makeHttpRequest(url, "GET",
contactList);
try {
// Getting Array of Contacts
hospital = new JSONArray(json);
if (hospital != null) {
// looping through
for (int i = 0; i < hospital.length(); i++) {
JSONObject contacts = hospital.getJSONObject(i);
// Storing each json item in variable
String id = contacts.getString(TAG_HID);
String name = contacts.getString(TAG_HNAME);
String address = contacts.getString(TAG_HADDRESS);
String phone1=contacts.getString(TAG_HPHONE1);
String phone2=contacts.getString(TAG_HPHONE2);
String fax=contacts.getString(TAG_HFAX);
String email=contacts.getString(TAG_HEMAIL);
String url=contacts.getString(TAG_URL);
String hlongitude=contacts.getString(TAG_LONGITUDE);
String hlatitude=contacts.getString(TAG_LATITUDE);
String hdistance=contacts.getString(TAG_HDISTANCE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_HID, id);
map.put(TAG_HNAME, name);
map.put(TAG_HADDRESS, address);
map.put(TAG_HPHONE1, phone1);
map.put(TAG_HPHONE2, phone2);
map.put(TAG_HFAX, fax);
map.put(TAG_HEMAIL, email);
map.put(TAG_URL, url);
map.put(TAG_LONGITUDE, hlongitude);
map.put(TAG_LATITUDE, hlatitude);
map.put(TAG_HDISTANCE, hdistance);
// adding HashList to ArrayList
contactList.add(map);
}
}
}
catch (JSONException e) {
e.printStackTrace();
}
some coding for JSON Parser class
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public String makeHttpRequest(String url, String method,
ArrayList<HashMap<String, String>> contactList) {
// Making HTTP request
try {
// check for request method
if (method == "POST") {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity((List<? extends NameValuePair>) contactList));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
else if (method == "GET")
{
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format((List<? extends NameValuePair>) contactList, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
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());
}
// return JSON String
return json;
}
}