Tom S
Tom S

Reputation: 174

JSON String in android how to get the values

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

Answers (5)

Houcine
Houcine

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

keen
keen

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

umair
umair

Reputation: 607

Try This.

JSONArray arr = new JSONArray(result);
JSONObject jObj = arr.getJSONObject(0);
String tel1 = jObj.getString("tell");

Upvotes: 1

Chirag Ghori
Chirag Ghori

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

Nambi
Nambi

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

Related Questions