Reputation: 174
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?
Upvotes: 0
Views: 112
Reputation: 24181
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)
{
}
Upvotes: 0
Reputation: 3011
try
{
JSONArray jArray= new JSONArray(output);
JSONObject jsonObject=jsonArray.getJSONObject(0);
String tel= jsonObject.getString("tel1");
}catch(Exception e)
{
//error parsing response.
}
Upvotes: 2
Reputation: 607
Try This.
JSONArray arr = new JSONArray(result);
JSONObject jObj = arr.getJSONObject(0);
String tel1 = jObj.getString("tell");
Upvotes: 1
Reputation: 4231
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
}
Upvotes: 1
Reputation: 12042
Do like This
{- represents JsonObject
[- represents JsonArray
try
{
JSONArray jArray= new JSONArray(output);
JSONObject menuObject = JSONArray.getJSONObject(0);
String tel= menuObject.getString("tel1");
}catch(Exception e)
{
Log.e("d",e.getMessage());
}
Upvotes: 1