Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

json - Android Value ... of type java.lang.String cannot be converted to JSONArray

My project having a WCF to get records from database and return in JSON format like this:

{"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 a android app to consume the 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 breakpoint at jarray = jobj.getJSONArray("GetNotesResult"); and get this message from 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 JSON string and paste to an online JSON parser website at http://jsonviewer.stack.hu/ and it parsed well. Please help me to solve this problem!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your json the value of GetNotesResult is inclused inside "" so it's considered a string not an array..

to 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:

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...