java.lang.String类型的值不能转换为JSONArray [英] Value of type java.lang.String cannot be converted to JSONArray
问题描述
我已经花了2天,找到了问题的解决方案。
I've spent 2 days to find a solution with of problem.
下面是错误:
E/log_tag: Error parsing data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray
下面是JSON:
[
{
"Id": "5207fc6473516724343ce7a5",
"Name": "Эриван",
"Types": [
"Ресторан"
],
"Latitude": 53.904752,
"Longitude": 27.521095,
"OperatingTime": [
{
"Day": 1,
"Start": "10:00:00",
"Finish": "23:00:00"
},
{
"Day": 2,
"Start": "10:00:00",
"Finish": "23:00:00"
},
{
"Day": 3,
"Start": "10:00:00",
"Finish": "23:00:00"
},
{
"Day": 4,
"Start": "10:00:00",
"Finish": "23:00:00"
},
{
"Day": 5,
"Start": "10:00:00",
"Finish": "23:00:00"
},
{
"Day": 6,
"Start": "08:00:00",
"Finish": "23:00:00"
},
{
"Day": 0,
"Start": "08:00:00",
"Finish": "23:00:00"
}
],
"IsBookingAvailable": false
}]
获取字符串值类:
Class for getting String value:
public class JSONGet {
public static String getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONArray jArray = null;
// Download JSON data from URL
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
// Convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONArray(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return result;
}
}
在这里被转换成JSONArray:
And here is converting to JSONArray:
String jsonObjRecv = JSONGet.getJSONfromURL(URL_LIST);
JSONArray jsonArr = new JSONArray(jsonObjRecv);
我试图让JSON对象,然后将其转换成JSON数组,但我收到了同样的错误。
I was trying to get Json object then convert it into Json array but I received the same error.
推荐答案
现在的问题是,你的JSON是不正确的格式。我曾尝试与样品JSON和找到解决途径。现在,内置的JSONObject和JSONArray不能用来获得这样一个JSON响应。
The problem is that your JSON is in the incorrect format. I have tried with your sample JSON and found the solution to it. Now the inbuilt JSONObject and JSONArray cannot be used to get such a json response.
您需要从以下链接的 HTTPS://json-simple.google$c$c.com/files/json-simple-1.1.1.jar 。
You need to download a small library named "json-simple-1.1.1.jar" from this link https://json-simple.googlecode.com/files/json-simple-1.1.1.jar.
然后你就可以轻松地分析你的JSON和它不会给任何错误。我已经在做了一个小样本$ C $下你如何使用它:
Then you can parse your JSON easily and it won't give any error. I have made a small sample code for you on how to use it :
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
JSONParser parser_obj = new JSONParser();
JSONArray array_obj = (JSONArray) parser_obj.parse("String from web service");
// in your case it will be "result"
然后就可以处理它根据您的需要。
Then you can process it as per your need.
这篇关于java.lang.String类型的值不能转换为JSONArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!