0

在 Android 中解析 JSON 字符串时遇到问题。字符串本身看起来不错,至少它看起来像我打算返回的。但是我在尝试解析它时崩溃了。谁能明白为什么?谢谢!

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public JSONObject makeHttpRequest(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "n");
            }
            is.close();
            json = sb.toString();
            Log.e("JSON", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            System.err.println("start try3"); //<--fine here
            System.err.println(json);         //<--json string looks good
            jObj = new JSONObject(json);     
            System.err.println("done try3");  //<--never outputs
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

堆栈跟踪:

08-05 15:28:00.665: E/JSON Parser(20009): 解析数据时出错 org.json.JSONException: 值 [{"id":"3","bool_gets_sms":"0","picture_url":"无","email":"heather@me.com","cell":"12345","name":"Heather"},{"id":"7","bool_gets_sms":"0"," picture_url":"none","email":"alan@me.com","cell":"12335","name":"Alan"},{"id":"10","bool_gets_sms":" 0","picture_url":"none","email":"jenni@me.com","cell":"12345","name":"Jenni"},{"id":"11"," bool_gets_sms":"0","picture_url":"none","email":"jeff@me.com","cell":"12345","name":"Jeff"},{"id":"24","bool_gets_sms":" org.json.JSONArray 类型的 0","picture_url":"none","email":"rob@me.com","cell":"12345","name":"Rob"}] 无法转换到 JSON 对象

4

2 回答 2

0

您将实例化为 JSONObject,即 JSONArray,更改该数据类型和构造函数。

从您的堆栈跟踪:

of type org.json.JSONArray cannot be converted to JSONObject

所以,而不是这个:

static JSONObject jObj = null;
// ...
jObj = new JSONObject(json); 

这个:

static JSONArray jArr = null; //changed jObj to jArr for your naming conventions too...
// ...
jArr = new JSONArray(json); 

在发出请求之前,您应该知道服务器将返回哪种类型的数据组织,尽管您可以通过编程方式找到,但最好提前知道。JSON 非常简单,您可以在几分钟内通读规范... TL;DR:文本周围的花括号表示一个对象,方括号表示一个数组...

于 2013-08-05T21:39:48.053 回答
0

你应该这样做:

JsonElement el = new JsonParser().parse(json);
JSONObject obj= el.getAsJSONObject("the key you are looking for");

方法的确切名称可能因您用于 JSON 解析的库而异。注意在 JSONArray 而不是 JSONObject 中获取数组。

于 2013-08-05T21:43:15.080 回答