Android 格森投掷“;应为BEGIN_对象,但为BEGIN_数组”;?
我有以下JSON响应Android 格森投掷“;应为BEGIN_对象,但为BEGIN_数组”;?,android,json,json.net,gsoap,Android,Json,Json.net,Gsoap,我有以下JSON响应 {"message":"[{\"first_name\":\"Sushant\",\"last_name\":\"Bhatnagar\",\"receiver_id\":\"19\",\"docket_number\":\"Test12\",\"status\":\"open\"}]","code":200,"format":"json"} 我创建了两个类来解析它,如下所示:- public class JsonResponse implements Ser
{"message":"[{\"first_name\":\"Sushant\",\"last_name\":\"Bhatnagar\",\"receiver_id\":\"19\",\"docket_number\":\"Test12\",\"status\":\"open\"}]","code":200,"format":"json"}
我创建了两个类来解析它,如下所示:-
public class JsonResponse implements Serializable {
public String code;
public String format;
public List<Message> message;
}
使用GSOAP解析json,出现上述错误。解析JSON的代码是:-
public static JsonResponse readDockets(String mobileNumber) {
JsonResponse res = new JsonResponse();
HttpClient client = new DefaultHttpClient();
String service = "http://api.pod.iamwhiney.com:8994/api.php?path=/deliveryRecord/refresh/"+"9968395206";
HttpGet httpGet = new HttpGet(service);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity getResponseEntity = response.getEntity();
InputStream httpResponseStream = getResponseEntity.getContent();
Reader inputStreamReader = new InputStreamReader(httpResponseStream);
Gson gson = new Gson();
res = gson.fromJson(inputStreamReader, JsonResponse.class);
} else {
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
您的
JSon
字符串应采用以下格式:
{
sections:
[
{
"SectionId": 1,
"SectionName": "Android"
}
]
}
我不知道为什么要使用Gson,因为Android有自己的内置JSON解析器。至于您得到的错误……这是因为您正在解析的JSON是JSONArray,而不是JSONObject。我不太确定@Yaqub在看什么,但您的JSON响应应该如下所示:
{"message":
{"first_name":"Sushant",
"last_name":"Bhatnagar".....
"status":"open"
},"code":"200","format":"json"}
也就是说,内容周围没有[],因为这会告诉JSON解析器它是一个只有1个索引的JSON数组,而您显然需要一个JSON对象。上面的JSONString将允许您在可以从“message”标记获取JSONObject的位置对其进行解析
注意:我删除了转义,因为我想通过解析器运行我的编辑,但您可以轻松地将它们添加回,它应该仍然可以工作
注意:原始JSON中的“code”:200
必须是“code”:“200”
,否则会出现另一个错误
{"message":
{"first_name":"Sushant",
"last_name":"Bhatnagar".....
"status":"open"
},"code":"200","format":"json"}