I have this JSON string in my android app
[{"tel1":"999","tel2":"0790000000","tel3":"","interval":"60","deleteLocal":"1","id":"2"}]
How do I parse this into a JsonArray and then get the values, for example, tel1?
try
{
JSONArray jArray= new JSONArray(output);
JSONObject jsonObject=jsonArray.getJSONObject(0);
String tel= jsonObject.getString("tel1");
}catch(Exception e)
{
//error parsing response.
}
JSONObject jsonObject=jsonArray.getJSONObject(0);Try with this
try {
JSONArray jsonArray = new JSONArray(responsestring);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jobj = jsonArray.getJSONObject(i);
String strtel1 = jobj.getString("tel1");
Log.i("value tel1 : ", strtel1 + "");
}
} catch (JSONException e) {
// TODO: handle exception
}
JSONObject jobj = jsonArray.getJSONObject(i);JSONObject jsonObject=jsonArray.getJSONObject(0); ???try this :
try {
JSONArray jsonArray= new JSONArray(yourString);
if(jsonArray != null && jsonArray.length() > 0) {
for(int i=0; i< jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
if(json != null) {
String tel1 = json.optString("tel1", "default_value");
Log.d("PARSE", "tel1 : "+tel1);
}
}
}
}catch(Exception e)
{
}
json_decode()(with the second parameter set toTRUE): php.net/json_decode