-1

I've been trying to pass a String value from PHP to Android using JSON.

{"address":"Lebuh Bandar Utama, Bandar Utama,
            47800 Petaling Jaya, Selangor, Malaysia"}
{"address":"Jalan Pasar, Taman Bunga Kenanga,
            31000 Batu Gajah, Perak, Malaysia"}
{"address":"Jalan 1\/38a, Taman Sri Sinar,
            51200 Kuala Lumpur, Federal Territory of Kuala Lumpur, Malaysia"}

And following code to retrieve the String.

try {
    JSONArray jArray = new JSONArray(result);
    for(int i=0;i<jArray.length();i++){
        JSONObject json_data = jArray.getJSONObject(i);
        Log.i("log_tag","name: "+json_data.getString("address"));
        //Get an output to the screen
        a.add(json_data.getString("address"));
    }
}
catch(JSONException e) {
    Log.e("log_tag", "Error parsing data "+e.toString());
}

The error is "JSONObject cannot be converted into JSONArray". Please shed some light and thanks in advance.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user3153745
  • 133
  • 4
  • 13
  • your json is not valid check it in jslint – Nambi Jan 22 '14 at 08:19
  • First, this has been answered numerous times on SO ... will pick a dup. Second, you don't even have valid JSON; *nothing* you do will make that parse. – Brian Roach Jan 22 '14 at 08:21
  • or http://stackoverflow.com/questions/8018931/jsonobject-cannot-be-converted-to-jsonarray?rq=1 or http://stackoverflow.com/questions/9499629/android-jsonobject-cannot-be-converted-to-jsonarray?rq=1 or ... – Brian Roach Jan 22 '14 at 08:23
  • @user3153745 it showing :SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data not valid format – Ravind Maurya Jan 22 '14 at 08:24
  • check here http://json.parser.online.fr/ your json string which you want to parse – Ravind Maurya Jan 22 '14 at 08:27

3 Answers3

1

For converting to JSON ARRAY, you should have json address format like below:

{
    "address":
    [
        {"Lebuh Bandar Utama, Bandar Utama,
            47800 Petaling Jaya, Selangor, Malaysia"},
        {"Jalan Pasar, Taman Bunga Kenanga,
            31000 Batu Gajah, Perak, Malaysia"},
        {"Jalan 1\/38a, Taman Sri Sinar,
            51200 Kuala Lumpur, Federal Territory of Kuala Lumpur, Malaysia"}
    ]
}

Then your code will be like below:

try{
    JSONObject obj= new JSONObject(result);
    JSONArray jArray = obj.getJSONArray("address");

    //DO ANYTHING WITH jARRAY NOW
}
catch(JSONException e){
    Log.e("log_tag", "Error parsing data "+e.toString());
}

Then you can convert Address to JSONARRAY..

Hope it will help you..!!!

Rushabh Patel
  • 3,052
  • 4
  • 26
  • 58
  • i have same problem ............now i have use your code where i found result ask to create local variable can i define local variable in this case ....... – Amitsharma Feb 25 '14 at 07:46
  • i have use arraylist = new ArrayList>(); String url="http://thelinejunk.com/mobileapi//locationlistdata.php"; jsonobject = JSONfunctions .getJSONfromURL(url); try { String address = null; – Amitsharma Feb 25 '14 at 07:49
  • jsonobject= new JSONObject(address); jsonarray = jsonobject.getJSONArray(address); for (int i = 0; i < jsonarray.length(); i++) { HashMap map = new HashMap(); jsonobject = new JSONObject("result"); – Amitsharma Feb 25 '14 at 07:50
0

Make sure you have a valid json

You can check here

http://jsonlint.com/

{ represents a json object node

[ represents a json array node

If result is a valid json and is a JSONObject then

JSONObject jsonobect = new JSONObject(result)

Or your json looks something like

[ // json array node
    {  // json object node
        "address": "Lebuh Bandar Utama, Bandar Utama,47800 Petaling Jaya, Selangor, Malaysia"
    },
    {
        "address": "Jalan Pasar, Taman Bunga Kenanga,31000 Batu Gajah, Perak, Malaysia"
    },
    {
        "address": "Jalan 1/38a, Taman Sri Sinar,51200 Kuala Lumpur, Federal Territory of Kuala Lumpur, Malaysia"
    }
]

or something like

{ // json object node
    "addresses": [ // json array of addresses
        { // json object node
            "address": "Lebuh Bandar Utama, Bandar Utama,47800 Petaling Jaya, Selangor, Malaysia"
        },
        {
            "address": "Jalan Pasar, Taman Bunga Kenanga,31000 Batu Gajah, Perak, Malaysia"
        },
        {
            "address": "Jalan 1/38a, Taman Sri Sinar,51200 Kuala Lumpur, Federal Territory of Kuala Lumpur, Malaysia"
        }
    ]
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

JsonArray representation is [{JSONObject},{JSONOBject}] but you have {JSONOBject}{JSONOBject}, so you don't have jsonArray

Eddy
  • 489
  • 4
  • 10