JSONException:java.lang.String 类型的值无法转换为 JSONObject [英] JSONException: Value of type java.lang.String cannot be converted to JSONObject

查看:41
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆