How to Parse Jsonarray in Android

how to parse JSONArray in android

If you're after the 'name', why does your code snippet look like an attempt to get the 'characters'?

Anyways, this is no different from any other list- or array-like operation: you just need to iterate over the dataset and grab the information you're interested in. Retrieving all the names should look somewhat like this:

List<String> allNames = new ArrayList<String>();

JSONArray cast = jsonResponse.getJSONArray("abridged_cast");
for (int i=0; i<cast.length(); i++) {
JSONObject actor = cast.getJSONObject(i);
String name = actor.getString("name");
allNames.add(name);
}

(typed straight into the browser, so not tested).

Android : parse a JSONArray

Here is your code to parse data,

private void parseData(){
try {
JSONArray jsonArray=new JSONArray(response);
JSONObject jsonObject=jsonArray.getJSONObject(0);
JSONArray jsonArrayNid=jsonObject.getJSONArray("nid");
JSONArray jsonArrayUid=jsonObject.getJSONArray("uid");
JSONArray jsonArrayField_image=jsonObject.getJSONArray("field_image");
for(int i=0;i<jsonArrayNid.length();i++){
JSONObject jsonObjectNid=jsonArrayNid.getJSONObject(i);
String value=jsonObjectNid.getString("value"); //here you get your nid value
}
for(int i=0;i<jsonArrayUid.length();i++){
JSONObject jsonObjectUid=jsonArrayUid.getJSONObject(i);
String target_id=jsonObjectUid.getString("target_id"); //here you get your uid target_id value
String url=jsonObjectUid.getString("url"); //here you get your uid url value
}
for(int i=0;i<jsonArrayField_image.length();i++){
JSONObject jsonObjectFieldImage=jsonArrayField_image.getJSONObject(i);
String target_id=jsonObjectFieldImage.getString("target_id");
String alt=jsonObjectFieldImage.getString("alt");
String title=jsonObjectFieldImage.getString("title");
String width=jsonObjectFieldImage.getString("width");
String height=jsonObjectFieldImage.getString("height");
String url=jsonObjectFieldImage.getString("url");
}
} catch (JSONException e) {
e.printStackTrace();
}

}

Parsing JSON array of objects in android

First be sure you have retrieved JSON String,

Then You can easily access the properties inside them like

try {
JSONArray jsonArray = new JSONArray(json);
JSONObject obj = jsonArray.getJSONObject(0); //0 for just retrieving first object you can loop it
String myVehicleID = obj.getString("vehicleId"); //To retrieve vehicleId
//Similarly do it for others as well
} catch (JSONException e) {
e.printStackTrace();
}

Parse Json Array Inside Json Array with android

Try something like this:

JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
recievedUser = new Parent();
recievedUser.setMenu_Cat_Id(jsonObject.getInt("Menu_Cat_Id"));
recievedUser.setMenu_Cat_Name(jsonObject.getString("Menu_Cat_Name"));
recievedUser.setPhoto(jsonObject.getString("photo"));
categories = new JSONArray(jsonObject.get("categories"));

for (int i = 0; i < categories.length(); i++) {
category = categories.getJSONobject(i);
child=new Parent();
child.setItem_NameEN(categories.getString("Item_NameEN"));
listChildData.add(child);
}

listDataHeader.add(recievedUser);
}

How to parse this Json Array in android

You BufferReader is not working as expected in your class JSONfunctions , do some changes in your class and it should looks like

JSONfunctions.java

public class JSONfunctions {

public static JSONArray getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONArray jArray = null;

// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}


StringBuffer sb = new StringBuffer("");
try {
URL urls = new URL(url);
URLConnection urlConnection;
urlConnection = urls.openConnection();
InputStream in = urlConnection.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
System.out.println(sb);
result=sb.toString();
} catch (IOException e) {}
catch (Exception e) {}
try {

jArray = new JSONArray(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}

return jArray;
}
}

and to avoid Exception check if JSONArray is not null then only it goes to run for loop like this :

 JSONArray ja2 =   JSONfunctions.getJSONfromURL("http://app.ireff.in:9090/IreffWeb/android?  service=airtel&circle=assam");
if(ja2!=null)
for (int i = 0; i < ja2.length(); i++) {}

Parsing JSON array and object in Android

Your JSON string start with JSONArray.

Here sample code, try it.

    JSONArray mJsonArray = new JSONArray(jsonStr);
JSONObject mJsonObject = mJsonArray.getJSONObject(0);

String pmid = mJsonObject.getString("pmid");
String name = mJsonObject.getString("name");
String result = mJsonObject.getString("result");


JSONArray mJsonArrayProperty = mJsonObject.getJSONArray("properties");
for (int i = 0; i < mJsonArrayProperty.length(); i++) {
JSONObject mJsonObjectProperty = mJsonArrayProperty.getJSONObject(i);

String prop_id = mJsonObjectProperty.getString("prop_id");
String prop_name = mJsonObjectProperty.getString("prop_name");
String address = mJsonObjectProperty.getString("address");
String city = mJsonObjectProperty.getString("city");
String state = mJsonObjectProperty.getString("state");
String zip = mJsonObjectProperty.getString("zip");
String lat = mJsonObjectProperty.getString("lat");
String lon = mJsonObjectProperty.getString("long");
}

Check Android JSON Parsing Tutorial

How to parse JSON Array (Not Json Object) in Android

use the following snippet to parse the JsonArray.

JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
String url = jsonobject.getString("url");
}

how to parse json array in android using volley

Here is

 //Here is main response object
JSONObject json = new JSONObject(response);

//now get your json array like this
JSONArray booking = json.getJSONArray("savebooking");

// now check the array length > 0

if(booking.length ()> 0){

for(int countItem = 0;countItem<booking.length;countItem++){

JSONObject bookingObject = booking.getJsonObject(countItem);

String id = bookingObject.isNull("id")?"":bookingObject.optString("id");
list.add(id);


}

}


Related Topics



Leave a reply



Submit