【发布时间】:2018-10-24 09:15:08
【问题描述】:
我正在尝试解析从我的烧瓶服务器检索到的数组。数组如下。
"[{\"firstName\": \"Roy\", \"lastName\": \"Augustine\"}]"
我正在使用以下代码在 Android Studio 中解析数组。
private void loadDrives() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://142.93.216.24:5000/",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
//adding the product to product list
driveList.add(new TestMl(
jsonObject.getString("firstName"),
jsonObject.getString("lastName")
));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
但是,我收到以下错误。
org.json.JSONException: Value [{"firstName": "Roy", "lastName": "Augustine"}] of type java.lang.String cannot be converted to JSONObject
错误提示json被成功检索,但无法成功解析数组。有人可以提出一个合适的解决方案吗?
【问题讨论】:
-
即使转换为 jsonArray 后,它仍然给我同样的错误。检查我更新的代码@PrasanthS
-
您是否删除了 JSON 响应开头和结尾的双引号?
-
Value [{"firstName": "Roy", "lastName": "Augustine"}] of type java.lang.String这表明您的 JSON 不是对象或数组,而是它的String -
如何解析字符串?那是我的问题@AliAhmed
-
您正在使用一些代码生成此 JSON。你必须从你的服务器处理这个
标签: android json parsing android-volley