6

我的项目有一个 WCF 从数据库中获取记录并以 JSON 格式返回,如下所示:

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

我还有一个使用 JSON 的 android 应用程序,这是我的代码:

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;
    }

我设置断点jarray = jobj.getJSONArray("GetNotesResult");并从以下位置获取此消息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

我试图复制 JSON 字符串并粘贴到http://jsonviewer.stack.hu/的在线 JSON 解析器网站,它解析得很好。请帮我解决这个问题!

4

2 回答 2

20

在您的 json 中, 的值GetNotesResult包含在其中,""因此它被视为字符串而不是数组。

被解析为一个数组,它应该是..

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

所以解决方案是两件事之一:

  1. 如果您可以修改服务器响应,请删除""json 数组周围的 from 。或者
  2. 首先将其解析为字符串,然后从该字符串创建一个 json 数组。

    String notes = jobj.getString("GetNotesResult");
    jarray = new JSONArray(notes);
    
于 2013-04-11T09:40:41.630 回答
0

我尝试将其粘贴到http://jsonviewer.stack.hu/中,但没有成功。

为了使它工作,我必须替换所有\"to然后从之前和"之后删除"符号[]

像这样:

{"GetNotesResult":[{"ID":1,"Title":"Note1","Content":"HelloVuChienThang","CreatedBy":"thangvc"},{"ID":2,"Title":"Note2","Content":"HelloNguyenThiNgoc","CreatedBy":"thangvc"}]}
于 2013-04-11T09:40:59.927 回答