-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.

5
  • your json is not valid check it in jslint Commented Jan 22, 2014 at 8: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. Commented Jan 22, 2014 at 8:21
  • or stackoverflow.com/questions/8018931/… or stackoverflow.com/questions/9499629/… or ... Commented Jan 22, 2014 at 8:23
  • @user3153745 it showing :SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data not valid format Commented Jan 22, 2014 at 8:24
  • check here json.parser.online.fr your json string which you want to parse Commented Jan 22, 2014 at 8:27

3 Answers 3

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..!!!

Sign up to request clarification or add additional context in comments.

3 Comments

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 .......
i have use arraylist = new ArrayList<HashMap<String, String>>(); String url="thelinejunk.com/mobileapi//locationlistdata.php"; jsonobject = JSONfunctions .getJSONfromURL(url); try { String address = null;
jsonobject= new JSONObject(address); jsonarray = jsonobject.getJSONArray(address); for (int i = 0; i < jsonarray.length(); i++) { HashMap<String, String> map = new HashMap<String, String>(); jsonobject = new JSONObject("result");
0

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

Comments

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"
        }
    ]
}

2 Comments

This is correct ... if the OP even had a valid JSON Object.
@BrianRoach yup i realized it now op has not posted a valid json

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.