JSONException: Value of type java.lang.String cannot be converted to JSONObject(JSONException:java.lang.String 类型的值无法转换为 JSONObject)
问题描述
我有一个包含 2 个 JSON 数组的 JSON 文件:一个数组用于路线,一个数组用于景点.
一条路线应该由用户导航到的多个景点组成.不幸的是,我收到了错误:
JSONException:java.lang.String 类型的值无法转换为 JSONObject
这是我的变量和解析 JSON 文件的代码:
private InputStream is = null;私有字符串 json = "";私有 JSONObject jObj = null;尝试 {BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);StringBuilder sb = new StringBuilder();字符串线=空;while ((line = reader.readLine()) != null) {sb.append(line + "
");}is.close();//hier habe ich das JSON-File als Stringjson = sb.toString();Log.i("JSON 解析器", json);} 捕捉(异常 e){Log.e("缓冲区错误", "转换结果错误" + e.toString());}//尝试将字符串解析为 JSON 对象尝试 {jObj = 新的 JSONObject(json);} 捕捉(JSONException e){Log.e("JSON Parser", "解析数据出错" + e.toString());}//返回 JSON 字符串返回 obj;}
Log.i("JSON 解析器", json);显示在生成的字符串的开头有一个奇怪的符号:
但错误发生在这里:
试试{jObj = 新的 JSONObject(json);} 捕捉(JSONException e){Log.e("JSON Parser", "解析数据出错" + e.toString());}
<块引用>
04-22 14:01:05.043: E/JSON Parser(5868): 解析数据时出错org.json.JSONException: Value//STRANGE SIGN HERE//类型java.lang.String 无法转换为 JSONObject
任何人都知道如何摆脱这些标志以创建 JSONObject?
看这个http://stleary.github.io/JSON-java/org/json/JSONObject.html#JSONObject-java.lang.String-
JSON 对象
public JSONObject(java.lang.String source)抛出 JSONException
从源 JSON 文本字符串构造一个 JSONObject.这是最常用的`JSONObject构造函数.
参数:source - `以 {(左大括号)开头并以 }(右大括号)结尾的字符串.抛出:JSONException - 如果源字符串中存在语法错误或重复键.
你尝试使用类似的东西:
new JSONObject("{你的字符串}")
I have a JSON file with 2 JSON-Arrays in it: One Array for routes and one Array for sights.
A route should consist of several sights where the user gets navigated to. Unfortunately I am getting the error:
JSONException: Value of type java.lang.String cannot be converted to JSONObject
Here are my variables and the code that parses the JSON-File:
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 + "
");
}
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 Parser", json); shows me that at the beginning of the generated string there is a strange sign:
but the error happens here:
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 Parser(5868): Error parsing data org.json.JSONException: Value //STRANGE SIGN HERE // of type java.lang.String cannot be converted to JSONObject
anybody has a clue on how to get rid of these signs in order to create the JSONObject?
see this http://stleary.github.io/JSON-java/org/json/JSONObject.html#JSONObject-java.lang.String-
JSONObject
public JSONObject(java.lang.String source)
throws JSONException
Construct a JSONObject from a source JSON text string. This is the most commonly used` JSONObject constructor.
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.
you try to use some thing like:
new JSONObject("{your string}")
这篇关于JSONException:java.lang.String 类型的值无法转换为 JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JSONException:java.lang.String 类型的值无法转换为 JSONObject
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01