JSONException:类型java.lang.String的值不能转换为JSONObject

我有一个JSON文件,其中有2个JSON数组:一个数组的路线和一个数组的景点。

一条路线应该由用户可以导航到的几个景点组成。 不幸的是我得到的错误:

JSONException:类型java.lang.String的值不能转换为JSONObject

这里是我的变量和解析JSON文件的代码:

private InputStream is = null; private String json = ""; private JSONObject jObj = null; 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(); // hier habe ich das JSON-File als String json = sb.toString(); Log.i("JSON Parser", 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; } 

Log.i(“JSON解析器”,json); 告诉我,在生成的字符串的开头有一个奇怪的标志: 在这里输入图像描述

但错误发生在这里:

 try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } 

04-22 14:01:05.043:E / JSON解析器(5868):解析数据时出错org.json.JSONException:// // STRANGE SIGN HERE // //类型java.lang.String无法转换为JSONObject

任何人都有一个线索,如何摆脱这些迹象,以创建JSONObject?

看到这个http://stleary.github.io/JSON-java/org/json/JSONObject.html#JSONObject-java.lang.String-

的JSONObject

 public JSONObject(java.lang.String source) throws JSONException 

从源JSON文本字符串构造JSONObject。 这是最常用的`JSONObject构造函数。

 Parameters: source - `A string beginning with { (left brace) and ending with } (right brace).` Throws: JSONException - If there is a syntax error in the source string or a duplicated key. 

你尝试使用一些东西,如:

 new JSONObject("{your string}") 

原因是在撰写字符串时添加了一些不想要的字符。 临时解决方案是

 return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1)); 

但是尝试删除源字符串上的隐藏字符。

几天以来都有同样的问题。 终于找到了解决办法 。 PHP服务器返回了一些看不见的字符,在LOG或System.out中看不到。

所以解决的办法是,我试图substring我的json字符串一个一个,当我来到子字符串(3)错误消失。

BTW。 我在两边都使用了UTF-8编码。 PHP方面: header('Content-type=application/json; charset=utf-8');

JAVA端: BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);

所以一个一个尝试解决方案1,2,3,4 …! 希望它可以帮助你们!

 try { jObj = new JSONObject(json.substring(3)); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data [" + e.getMessage()+"] "+json); } 

这里是UTF-8版本,有几个异常处理:

 static InputStream is = null; static JSONObject jObj = null; static String json = null; static HttpResponse httpResponse = null; public JSONObject getJSONFromUrl(String url) { // Making HTTP request try { HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 10000); HttpConnectionParams.setSoTimeout(params, 10000); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); HttpProtocolParams.setUseExpectContinue(params, true); // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(params); HttpGet httpPost = new HttpGet( url); httpResponse = httpClient.execute( httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException ee) { Log.i("UnsupportedEncodingException...", is.toString()); } catch (ClientProtocolException e) { Log.i("ClientProtocolException...", is.toString()); } catch (IOException e) { Log.i("IOException...", is.toString()); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "utf-8"), 8); //old charset iso-8859-1 StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); reader.close(); json = sb.toString(); Log.i("StringBuilder...", 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 (Exception e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); try { jObj = new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1)); } catch (Exception e0) { Log.e("JSON Parser0", "Error parsing data [" + e0.getMessage()+"] "+json); Log.e("JSON Parser0", "Error parsing data " + e0.toString()); try { jObj = new JSONObject(json.substring(1)); } catch (Exception e1) { Log.e("JSON Parser1", "Error parsing data [" + e1.getMessage()+"] "+json); Log.e("JSON Parser1", "Error parsing data " + e1.toString()); try { jObj = new JSONObject(json.substring(2)); } catch (Exception e2) { Log.e("JSON Parser2", "Error parsing data [" + e2.getMessage()+"] "+json); Log.e("JSON Parser2", "Error parsing data " + e2.toString()); try { jObj = new JSONObject(json.substring(3)); } catch (Exception e3) { Log.e("JSON Parser3", "Error parsing data [" + e3.getMessage()+"] "+json); Log.e("JSON Parser3", "Error parsing data " + e3.toString()); } } } } } // return JSON String return jObj; } 

我认为这个问题可能在你正在尝试使用的字符集中。 最好使用UTF-8而不是iso-8859-1。

同时打开任何文件正在使用您的InputStream,并确保没有特殊字符被意外插入。 有时你必须特别告诉你的编辑器显示隐藏/特殊字符。

 return response; 

之后得到我们需要解析这个答复:

 JSONObject myObj=new JSONObject(response); 

答复时不需要双引号。

这是简单的方法(谢谢Gson)

 JsonParser parser = new JsonParser(); String retVal = parser.parse(param).getAsString(); 

https://gist.github.com/MustafaFerhan/25906d2be6ca109f61ce#file-evaluatejavascript-string-problem

这对我有效

 json = json.replace("\\\"","'"); JSONObject jo = new JSONObject(json.substring(1,json.length()-1)); 

我做了这个改变,现在它适用于我。

 //BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8), 8); 

json字符串开始处的3个字符对应于字节顺序掩码(Byte Order Mask,BOM),字节顺序掩码(Byte Order Mask,BOM)是字节序列,用于将文件标识为UTF8文件。

确保发送json的文件使用utf8(no bom)编码进行编码。

(我有与TextWrangler编辑器相同的问题,使用另存为 – utf8(无bom)强制正确的编码。)

希望它有帮助。

在我的情况下,问题发生在php文件。 它给了不需要的字符。这就是为什么发生json parsing问题。

然后,我将我的php code粘贴到Notepad++然后Encode in utf-8 without BOM Encoding选项卡中选择Encode in utf-8 without BOMEncoding然后运行这个代码 –

我的问题消失了。

就我而言,我的Android应用程序使用Volley将托管在Microsoft Azure上的API应用程序与空的主体进行POST调用。

错误是:

 JSONException: Value <p>iisnode of type java.lang.String cannot be converted to JSONObject 

这是我如何构建Volley JSON请求的一个片段:

 final JSONObject emptyJsonObject = new JSONObject(); JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, emptyJsonObject, listener, errorListener); 

我解决了我的问题,通过创建一个空的JSON对象的JSONObject ,如下所示:

 final JSONObject emptyJsonObject = new JSONObject("{}"); 

我的解决方案是沿着这个旧的答案 。