0

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?

3
  • If you're trying to do it with PHP, use json_decode() (with the second parameter set to TRUE): php.net/json_decode Commented Jan 23, 2014 at 12:26
  • @AmalMurali, OP wants to traverse the JSON generated from PHP into his Android App. Commented Jan 23, 2014 at 12:28
  • @ShankarDamodaran: Ah, I see. I wasn't sure about that - which is why added "If you're trying to do it with PHP" at the beginning. Commented Jan 23, 2014 at 12:30

5 Answers 5

2
try
    {
       JSONArray jArray= new JSONArray(output);
       JSONObject jsonObject=jsonArray.getJSONObject(0);
       String tel= jsonObject.getString("tel1");
    }catch(Exception e)
    {
     //error parsing response.
    }
Sign up to request clarification or add additional context in comments.

2 Comments

if jArray having no data means length = 0 then what happen at line JSONObject jsonObject=jsonArray.getJSONObject(0);
you can check just before 2nd line if(jArray.length()) then continue parsing.
1

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());
        }

Comments

1

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
    }

3 Comments

I get this error Cannot make a static reference to the non-static method getJSONObject(int) from the type JSONArray
sorry i made a mistake now check my answer ...use this JSONObject jobj = jsonArray.getJSONObject(i);
@TomS : Heyy in your accepted answer if jArray having no data means length = 0 then what happen at line JSONObject jsonObject=jsonArray.getJSONObject(0); ???
1

Try This.

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

Comments

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)
{
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.