我花了 2 天的时间来找到问题的解决方案。
这是错误:
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
}]
获取字符串值的类:
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:
String jsonObjRecv = JSONGet.getJSONfromURL(URL_LIST);
JSONArray jsonArr = new JSONArray(jsonObjRecv);
我试图获取 Json 对象,然后将其转换为 Json 数组,但我收到了同样的错误。