[英]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.