简体   繁体   English

JSONException - 将字符串解析为JSONArray

[英]JSONException - Parsing String to JSONArray

i want to parse some json downloaded from the web in a JSONArray. 我想解析从JSONArray中下载的一些json。 Simple thing i think, but can't get it to work. 我认为简单的事情,但不能让它工作。

It seams that the JSON Format is the Problem. 它接缝JSON格式是问题。 I tried different ways to fix it, but nothing helps. 我尝试了不同的方法来解决它,但没有任何帮助。

I download the String with this method: 我用这种方法下载String:

private String DownloadContent(String URL) {

    String result = null;
    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        HttpResponse response = httpClient.execute(httpGet);
        result = EntityUtils.toString(response.getEntity());
    } catch(Exception e) {
        Log.e("log_tag", e.toString());
    }
    return result;        
}

After that i try to parse the string to the JSONArray like this: 之后,我尝试将字符串解析为JSONArray,如下所示:

String resultString = DownloadContent(stringUrl);
JSONArray jArray = new JSONArray(resultString);

but everytime i get this exception: 但每次我得到这个例外:

例外

i test the following to get the JSON right: 我测试以下内容以获得正确的JSON:

result = EntityUtils.toString(response.getEntity(), "UTF-8");
result = StringEscapeUtils.unescapeJson(EntityUtils.toString(response.getEntity()));

this methods changed the string but no time the JSON is valid (tested with this ). 这个方法改变了字符串,但没有时间JSON有效(用测试)。

In my JSON are things like this: 在我的JSON中有这样的事情:

Meine gr\\u00f6\\u00dfte Leidenschaft ist es
http:\\/\\/bla.de\\/wp-content\\/uploads\\/2014\\/02\\/hello.jpg

What should i do to get it right? 我该怎么做才能搞定? I hope someone can help me :) Thanks! 我希望有人可以帮助我:)谢谢!

Looking at the detailMessage , it looks like you are trying to create a JSONArray from JSON that looks like 查看detailMessage ,看起来您正在尝试从JSON创建一个看起来像的JSONArray

{"posts": [...]}

That's a JSON object, not an array. 那是一个JSON对象,而不是一个数组。 You'll need to parse it as a JSONObject and get the array object from it. 您需要将其解析为JSONObject并从中获取数组对象。

JSONObject object = new JSONObject(resultString);
JSONArray array = object.getJSONArray("posts"); // or whatever the method is to get a JSONArray element from the JSONObject

This is assuming you are trying to get the JSON array named posts . 这假设您正在尝试获取名为posts的JSON数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JSONException字符串无法转换为JSONArray - JSONException String cannot be converted to JSONArray 解析类型为java.lang.String的data.org.json.JSONException值时出错,无法转换为JSONArray - Error parsing data.org.json.JSONException value of type java.lang.String cannot be converted to JSONArray 解析数据org.json.JSONException时出错:类型java.lang.String的值无法转换为JSONArray - Error Parsing Data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray org.json.JSONException:JSONArray [0]不是字符串 - org.json.JSONException: JSONArray[0] not a string org.json.JSONException:JSONArray [0]不是字符串异常 - org.json.JSONException: JSONArray[0] not a string Exception JSonArray转换为String以使用GSon进行解析 - JSonArray to String for parsing with GSon ClassCastException将字符串解析为JSONArray - ClassCastException Parsing string to JSONArray 解析数据org.json.JSONException时出错:值 - Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray JSONException:无法将类型为java.lang.String的值转换为JSONArray - JSONException: Value of type java.lang.String cannot be converted to JSONArray 检索JSONArray中的元素时出现JSONException(来自json字符串) - JSONException while retrieving elements in JSONArray (from json string)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM