0

I'm having problem with string cannot convert to JSONObject.Anyone could help in solving this problem? Thanks and very appreciate for helping.

protected void onPostExecute(String result) {           
if (result==null || result.length()==0){
            // no result:
            return;
}

//clear the list
moviesList.clear();

try {
    //turn the result into a JSON object
    JSONObject responseObject = new JSONObject("results");

            // get the JSON array named "results"
    JSONArray resultsArray = responseObject.getJSONArray(result);

    // Iterate over the JSON array: 
    for (int i = 0; i < resultsArray.length(); i++) {
        // the JSON object in position i 
        JSONObject messageObject = resultsArray.getJSONObject(i);

    // get the primitive values in the object
        String title = messageObject.getString("title");
        String details = messageObject.getString("synopsis");

        //put into the list:
            Movie movie = new Movie(title, details, null,null);
        moviesList.add(movie);
    }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    //refresh listView:
    adapter.notifyDataSetChanged();

    }
} 

result has value

the error is in the following line :

JSONObject responseObject = new JSONObject("results");
codercat
  • 22,873
  • 9
  • 61
  • 85
Lora
  • 25
  • 1
  • 8

4 Answers4

1
    String obj=JSONObject.quote(YourData);
    JSONArray lArray=new JSONArray(obj);

    // or simply  Delete the prefix 'results'  from your php Code
    // $res2=array("results"=>$response);
    // and you will retrive directelly your JsonArray like 

    JSONArray lArray=new JSONArray(YouData);
Wajdi Hh
  • 785
  • 3
  • 9
  • this is not working.... now the value is the long string of result and similar to previous: org.json.JSONException: Value {... }}]} of type org.json.JSONObject cannot be converted to JSONArray – Lora Nov 12 '13 at 23:47
0
  • That's because your format for JSONObject is wrong. See How to convert String to JSONObject in Java . Just "results" is not a JSON. Try and have something like:

    {"result":"blahblah"}

  • Or by mistake you have included double quotes in the result while writing

    JSONObject responseObject = new JSONObject("results");

    As result is already a string, try replacing the line with:

    JSONObject responseObject = new JSONObject(results);

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
0

Looks like you just mixed up these two lines, try it this way:

//turn the result into a JSON object
JSONObject responseObject = new JSONObject(result);

// get the JSON array named "results"
JSONArray resultsArray = responseObject.getJSONArray("results");

This is assuming the JSON response you get from somewhere contains a JSONObject, which in turn contains a JSONArray in 'results'.

Judging by the comments in your code sample, this is the case, it just is a case of a simple mixup due to similar naming.

bgse
  • 8,237
  • 2
  • 37
  • 39
0

Check if the JSON string itself begins with [ e.g.

[{"hello":"goodbye","name":"bob","age":26}]

if it does this means it is not a JSONObject but a JSONArray. Try changing

JSONObject responseObject = new JSONObject("results");

        // get the JSON array named "results"
JSONArray resultsArray = responseObject.getJSONArray(result);

to something like

JSONArray responseArray = new JSONArray("results");
JSONObject resonseObject = responseArray.toJSONObject(responseArray);
dodo
  • 156
  • 10