String类型不能转换为JSONArray

String类型不能转换为JSONArray

本文介绍了Android的价值... java.lang.String类型不能转换为JSONArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个WCF从数据库中获取记录,并以JSON格式返回像这样我的项目:

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

我也有一个Android应用程序消耗JSON,这是我的code:

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

我设置为 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字符串并粘贴到在线JSON解析器网站:并解析好。请帮我解决这个问题!在先进的感谢!

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! Thanks in advanced!

推荐答案

在您的JSON GetNotesResult 是在 inclused值所以它视作一个字符串不是一个数组。

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. 如果您可以修改服务器的响应,从角落找寻JSON数组删除

  2. 首先分析它的字符串,然后创建该字符串JSON数组..

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


这篇关于Android的价值... java.lang.String类型不能转换为JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:09