JSONException:java.lang.String类型的值不能转换到的JSONObject [英] JSONException: Value of type java.lang.String cannot be converted to JSONObject

查看:248
本文介绍了JSONException:java.lang.String类型的值不能转换到的JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2 JSON-阵列中有一个JSON文件: 一个阵列的路线,一个数组的目光。

一个路线应该包括几个景点,其中用户被导航到。 不幸的是我得到的错误:

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

下面是我的变量,code解析的JSON-文件:

 私人的InputStream是= NULL;
私人字符串JSON =;
私人的JSONObject jObj = NULL;

尝试 {
    的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(就是,ISO-8859-1),8);
    StringBuilder的SB =新的StringBuilder();
    串线= NULL;
    而((行= reader.readLine())!= NULL){
        sb.append(行+\ N);
    }
    is.close();
    //没有得票数HABE ICH DAS的JSON文件阿尔斯字符串
    JSON = sb.toString();
    Log.i(JSON解析器,JSON);
}赶上(例外五){
    Log.e(缓冲区错误,转换的结果错误+ e.toString());
}

//尝试解析字符串到一个JSON对象
尝试 {
    jObj =新的JSONObject(JSON);
}赶上(JSONException E){
    Log.e(JSON解析器,错误分析数据+ e.toString());
}

//返回JSON字符串
返回jObj;
}
 

Log.i(JSON解析器,JSON); 让我发现,在生成的字符串的开头有一个奇怪的符号:

但这个错误发生在这里:

 尝试{
    jObj =新的JSONObject(JSON);
}赶上(JSONException E){
    Log.e(JSON解析器,错误分析数据+ e.toString());
}
 

  

04-22 14:01:05.043:E / JSON解析器(5868):错误分析数据   org.json.JSONException:值// STRANGE在这里签字//类型   java.lang.String中无法转换为JSONObject的

任何人对如何让才能创建的JSONObject摆脱这些迹象线索?

解决方案

看这 <一href="http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject%28java.lang.String%29">http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject%28java.lang.String%29

的JSONObject

 公开的JSONObject(java.lang.String中源)
           抛出JSONException
 

从源JSON文本字符串构造一个JSONObject的。这是最常见的used` JSONObject的构造。

 参数:
    源 - '一个字符串{(左括号)和结尾}(右括号)开始.`
抛出:
    JSONException  - 如果在源字符串或复制的钥匙在一个语法错误。
 

您尝试使用了一些事情,如:

 新的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 + "\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 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://www.json.org/javadoc/org/json/JSONObject.html#JSONObject%28java.lang.String%29

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天全站免登陆