6

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!

3
  • That is not correct JsonArray Commented Apr 11, 2013 at 9:41
  • Of course I already added INTERNET permssion to manifest file. I even logged in to my android application with another function to connect with server Commented Apr 11, 2013 at 9:43
  • Dear Sameer, I use LINQtoSQL DataContext and JavaScriptSerializer to serialize the result and that is raw JSON I get from browser, could you tell me more about the problem of JSON format? Commented Apr 11, 2013 at 9:46

2 Answers 2

20

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);
    
Sign up to request clarification or add additional context in comments.

1 Comment

the \" also will not parse. See my answer.
0

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

to make it work I had to replace all the \" to " and then remove the " symbol from before the [ and after the ]

like this:

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

1 Comment

I'm not just paste the JSON I got in browser. I copied the value I got in JSONException that I mentioned above in my code and paste to jsonviewer.stack.hu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.