【问题标题】:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObjectorg.json.JSONException:java.lang.String 类型的值无法转换为 JSONObject
【发布时间】:2013-07-23 08:07:44
【问题描述】:

您好,我在 2.3 中遇到了 Android JSON Parser 的问题,在 4.0 中一切正常>。

我查看了他们在谈论编码(服务器)或其他服务器端的其他主题,但我尝试在我的所有 JSON 字段中放置“测试”,但问题仍然存在。

这是我的代码:

URL url = new URL(c.getString(R.string.url_ws) + url_ws);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
OutputStreamWriter request = new OutputStreamWriter(connection.getOutputStream());
request.write("&test=test");
request.flush();
request.close();


String line;
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();

while ((line = reader.readLine()) != null){
    sb.append(line + "\n");
}
String json = sb.toString().trim();
try {
     JSONObject obj = new JSONObject(json);
} catch (JSONException e) {
    e.printStackTrace();
    Log.d(Tools.TAG+"/debug JSONException", e.toString());
    return null;
}

这是我的例外

debug JSONException(4184): org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject

我看到“Value”和“of”之间有两个空格,所以我猜问题是一个空字符串,但我不明白。

谢谢。

编辑

这是我的 JSON 中的编码错误,

char a = json.substring(0, 1).charAt(0);
int ascii = (int)a;
Tools.myLog(">"+ascii+"<"); 

我找到了 65279 个字符

解决方案

Why is &#65279; appearing in my HTML?

在没有 BOM 的情况下编码 UTF-8。

【问题讨论】:

  • stackoverflow.com/questions/17792779/…。相似的!。发布你的 json。
  • JSONArray obj = new JSONArray(json);使用它而不是 JSONObject obj = new JSONObject(json);
  • 我的 JSON 不是数组:{"bHasError":false,"sSessionId":"5e10be14c408732179aa4731899882fd","iMembreId":7510064,"iCurrentDate":1374565720 ....
  • 你需要显示 JSON 响应
  • 在创建 JSONObject 之前记录字符串,以便查看它是否为 json 字符串。

标签: android json exception


【解决方案1】:
while ((line = reader.readLine()) != null){
    sb.append(line + "\n");
}

不如试试

while ((line = reader.readLine()) != null){
    sb.append(line);
   json = sb.toString().substring(0, sb.toString().length()-1);
}

按照这个方法获取json对象:

 public JSONObject getJSONObjFromUrl(String url, List<NameValuePair> params) {      
            System.out.println("url:: "+url );
            System.out.println("params:: "+ params +" " +params.get(0) );
            // Making HTTP request
            try {
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                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);
                }
                is.close();

                json = sb.toString().substring(0, sb.toString().length()-1);
               // 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 {
                jObj = new JSONObject(json);
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }

            // return JSON String

            return jObj;

        }

【讨论】:

  • 我删除了 \n 但你的子字符串技巧会出错:调试 JSONException(4461): org.json.JSONException: Unterminated object at character 242 of
  • 非常好的网站,我发现错误是 JSON 中 [" "] 之间的空格,我更新了我的问题。 TYVM !
猜你喜欢
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多