Reading a Json Array in android
A JSON Object starts with a {
and ends with a }
while a JSON Array starts with a [
and ends with a ]
.
In your case, change your code to have a JSONObject
instead.
JSONObject json = new JSONObject(jsonString);
JSONArray jArray = json.getJSONArray("list");
System.out.println("*****JARRAY*****" + jArray.length());
for(int i=0; i<jArray.length(); i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag", "_id" + json_data.getInt("account") +
", mall_name" + json_data.getString("name") +
", location" + json_data.getString("number") +
", telephone" + json_data.getString("url") +
",----" + json_data.getString("balance") +
",----" + json_data.getString("credit") +
",----" + json_data.getString("displayName")
);
}
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 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++) {}
Parse Json Array Inside Multiple Json Array with android
Your JSON String is composed of an array , which contains arrays, which contain objects. so
// parse json to an array
JSONArray array = new JSONArray(jsonString);
// loop through that array and get nested arrays
for (int i = 0; i < array.length(); i++) {
JSONArray subArray = array.getJSONArray(i);
// loop trhough those nested arrays to retrieve the objects
for (int j = 0; j < subArray.length(); j++) {
JSONObject obj = subArray.getJsonObject(j);
// parse the object properties...
}
}
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();
}
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 Studio: How to parse a JSON Object from Inside of a JSON Array?
By getting first node in array you have obtained address
node so no need to run loop
there, address
object is not an array you can get values directly. Try something like this:
JSONArray array = object.getJSONArray("pollingLocations");
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
String locationName = jsonObject.getJSONObject("address")
.getString("locationName");
Log.d("locationName", locationName);
JSONArray sourcesArray = jsonObject.getJSONArray("sources");
// run loop to get values from this array
for (int j = 0; j < sourcesArray.length(); j++){
JSONObject source = sourcesArray.getJSONObject(j);
String name = source.getString("name");
}
}
Related Topics
How to Get Elements of Fragments Created by Viewpager in Mainactivity
Android Change Material Elevation Shadow Color
Alertdialog from Within Broadcastreceiver? Can It Be Done
Android Get Date Before 7 Days (One Week)
What Exactly Is a Context in Java
Is It Ok to Save a JSON Array in Sharedpreferences
Android: Realm + Retrofit 2 + Gson
How to Get Data from Each Dynamically Created Edittext in Android
How to Set Seekbar Min and Max Value
Difference Between Actionbarsherlock and Actionbar Compatibility
Android Heterogeneous Gridview Like Pinterest
How to Get the Selected Value of a Spinner
How to Change Package Name of an Android Application