Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

i have json array created by php and i want to parse it in java/Android . my array dont have any keys and my this code could not work corretly

PHP Result:

Array
(
    [0] => Array
        (
            [id_] => 30340428
            [number] => 10001
            [mobile] => 0712200096
            [name] => 0
            [sms_body] => Hello World
            [date] => 2014-10-06 17:09:52
        )

    [1] => Array
        (
            [id_] => 30340428
            [number] => 10001
            [mobile] => 0712200096
            [name] => 0
            [sms_body] => Hello World
            [date] => 2014-10-06 17:09:52
        )

)

Json OutPut:

[{"id_":"30340428","number":"10001","mobile":"0712200096","name":"0","sms_body":"Hello World","date":"2014-10-06 17:09:52"},{"id_":"30340428","number":"10001","mobile":"0712200096","name":"0","sms_body":"Hello World","date":"2014-10-06 17:09:52"}] 

My Code:

try {
    String received = new JsonService(config_username,config_password,0,20,G.F_RECEIVE_SMS).request();
    JSONObject object = new JSONObject(received);

    for (int i = 0; i < object.length(); i++) {
        //String test2 = object.getJSONObject(i).getInt("sms_number") + "";
    }

} catch (Exception e) {
    e.printStackTrace();
}
share|improve this question

marked as duplicate by Anton Savin, LIUFA, Kurt Du Bois, bodi0, VMai Oct 7 '14 at 10:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers 3

up vote 2 down vote accepted

Change this..

JSONObject object = new JSONObject(received);

to

JSONArray data_array = new JSONArray(received);
for (int i = 0; i < data_array.length(); i++) {
   JSONObject json_obj = data_array.getJSONObject(i);
   String id = json_obj.getString("id");
}

because [ is JSONArray

[                 ==> JSONArray
    {             ==> JSONObject
        "id_": "30340428",
        "number": "10001",
        "mobile": "0712200096",
        "name": "0",
        "sms_body": "Hello World",
        "date": "2014-10-06 17:09:52"
    },
    {
        "id_": "30340428",
        "number": "10001",
        "mobile": "0712200096",
        "name": "0",
        "sms_body": "Hello World",
        "date": "2014-10-06 17:09:52"
    }
]
share|improve this answer
    
Yes, a JSONObject is either a JSONArray or a JSONElement (or a JSONPrimitive?) –  Davio Oct 7 '14 at 6:35
    
@Davio Yes Correct. –  Hariharan Oct 7 '14 at 6:39

Change JSONObject to JSONArray

try
{
String received = new JsonService(config_username,config_password,0,20,G.F_RECEIVE_SMS).request();
JSONArray object = new JSONArray(received);

for (int i = 0; i < object.length(); i++)
{
    //String test2 = object.getJSONObject(i).getInt("sms_number") + "";
     JSONObject json_obj = object.getJSONObject(i);
     String id = json_obj.getString("id");

}

 } catch (Exception e)
 {
  e.printStackTrace();
 }
share|improve this answer
/*response=
[{"id_":"30340428","number":"10001","mobile":"0712200096","name":"0","sms_body":"Hello World","date":"2014-10-06 17:09:52"},{"id_":"30340428","number":"10001","mobile":"0712200096","name":"0","sms_body":"Hello World","date":"2014-10-06 17:09:52"}]*/
// firstly get response in string after that response put  into Json array object show below
//here we cast string into json arrayObject
 JSONArray jsonArrayObj = new JSONArray(response);
// than make a loop getting json object from json array
for (int i = 0; i < jsonArrayObj.length(); i++)
{

     JSONObject jsonObject = jsonArrayObj.getJSONObject(i);
     // here we fetch value with key from json object
     String idStr = jsonObject.getString("id");
String numberStr = jsonObject.getString("number");
String mobileStr = jsonObject.getString("mobile");
}
 } catch (Exception e)
 {
  e.printStackTrace();
}*
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.