An Android ... value of type java.lang.String cannot be converted to a JSONArray

My project is with WCF to get records from a database and return in JSON format as follows:

{"GetNotesResult":"[{\"ID\":1,\"Title\":\"Note 1\",\"Content\":\"Hello Vu Chien Thang\",\"CreatedBy\":\"thangvc\"},{\"ID\":2,\"Title\":\"Note 2\",\"Content\":\"Hello Nguyen Thi Ngoc\",\"CreatedBy\":\"thangvc\"}]"} 

I also have an Android app to use JSON, and this is my code:

 private JSONArray getNotes(String UserName, String Password) { JSONArray jarray = null; JSONObject jobj = null; try{ StringBuilder builder = new StringBuilder(URL); builder.append("UserName=" + loggedInUser.getUserName()); builder.append("&"); builder.append("Password=" + loggedInUser.getPassword()); HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(builder.toString()); HttpResponse response = client.execute(httpGet); int status = response.getStatusLine().getStatusCode(); if(status==200) { HttpEntity entity = response.getEntity(); String data = EntityUtils.toString(entity,"utf-8"); jobj = new JSONObject(data); jarray = jobj.getJSONArray("GetNotesResult"); } else { Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show(); } } catch(ClientProtocolException e) { Log.d("ClientProtocol",e.getMessage()); } catch(IOException e) { Log.d("IOException", e.getMessage()); } catch(JSONException e) { Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); } catch(Exception e) { Log.d("Unhandle Error", e.getMessage()); } return jarray; } 

I set a breakpoint in jarray = jobj.getJSONArray("GetNotesResult"); and get this message from a JSONException :

 Value [{"ID":1,"Title":"Note 1","Content":"Hello Vu Chien Thang","CreatedBy":"thangvc"},{"ID":2,"Title":"Note 2","Content":"Hello Nguyen Thi Ngoc","CreatedBy":"thangvc"}] at GetNotesResult of type java.lang.String cannot be converted to JSONArray 

I tried to copy the JSON string and paste the JSON parser into the website at http://jsonviewer.stack.hu/ , and it was well versed. Please help me solve this problem!

+4
source share
2 answers

In your json, the value of GetNotesResult included inside "" , so it is considered a string, not an array.

which will be parsed as an array, it should be ...

 {"GetNotesResult":[{\"ID\":1,\"Title\":\"Note 1\",\"Content\":\"Hello Vu Chien Thang\",\"CreatedBy\":\"thangvc\"},{\"ID\":2,\"Title\":\"Note 2\",\"Content\":\"Hello Nguyen Thi Ngoc\",\"CreatedBy\":\"thangvc\"}]} 

So, the solution is one of two things:

  • If you can change the server response, remove the "" from the arround of the json array. or
  • first parse it as a string, and then create a json array from that string.

     String notes = jobj.getString("GetNotesResult"); jarray = new JSONArray(notes); 
+16
source

I tried pasting it in http://jsonviewer.stack.hu/ and it did not work.

for it to work, I had to replace everything \" with " , and then delete the character " before [ and after ]

like this:

 {"GetNotesResult":[{"ID":1,"Title":"Note1","Content":"HelloVuChienThang","CreatedBy":"thangvc"},{"ID":2,"Title":"Note2","Content":"HelloNguyenThiNgoc","CreatedBy":"thangvc"}]} 
0
source

All Articles