我正在尝试解析json但是它给了我错误java lang.string无法转换为jsonArray。这是我的json和错误:
Value {"MemberList":[{"MemberId":1,"FirmId":1,"MemberTypeId":1,
"MemberUserName":"test@example.com", "MemberName":"sth","UpdateDate":"\/Date(1421840040000)\/",
"LastLoginDate":"\/Date(1454995980000)\/",
"FirmTypeId":1,"FirmName":"Firm","FirmOfficialName":"Firm",
"FirmRowGuid":"sth","MemberRowGuid":"sth","AuthToken":"sth",
"IsRegistered":1}],"MemberPageList":null,"FirmModuleList":null,"Status":true,
"StatusCode":"OK"} of type java.lang.String cannot be converted to JSONArray
当我删除MemberList标头并手动运行它工作正常,但我必须用标头解析它我应该如何解析它。 userDetail是我的json字符串:
JSONArray jsonArray = new JSONArray(userDetail);
member = new Member();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
member.MemberId = jsonObject.optInt("MemberId");
member.MemberTypeId = jsonObject.optInt("MemberTypeId");
member.FirmTypeId = jsonObject.optInt("FirmTypeId");
member.IsRegistered = jsonObject.optInt("IsRegistered");
member.MemberUserName = jsonObject.optString("MemberUserName");
member.MemberName = jsonObject.optString("MemberName");
member.UpdateDate = jsonObject.optString("UpdateDate");
member.LastLoginDate = jsonObject.optString("LastLoginDate");
member.FirmName = jsonObject.optString("FirmName");
member.FirmOfficialName = jsonObject.optString("FirmOfficialName");
member.FirmRowGuid = jsonObject.optString("FirmRowGuid");
member.MemberRowGuid = jsonObject.optString("MemberRowGuid");
member.AuthToken = jsonObject.optString("AuthToken");
}
答案 0 :(得分:4)
它的json对象不是数组,所以改变,
JSONArray jsonArray = new JSONArray(userDetail);
到
JSONObject jsonObj= new JSONObject(userDetail);
JSONArray jsonArray = jsonObj.getJSONArray("MemberList");
要手动检查json,请使用此http://jsoneditoronline.org/
CODE:
JSONObject jsonObj= new JSONObject(userDetail);
JSONArray jsonArray = jsonObj.getJSONArray("MemberList");
for (int i = 0; i < jsonArray.length(); i++) {
member = new Member();
JSONObject jsonObject = jsonArray.getJSONObject(i);
member.MemberId = jsonObject.optInt("MemberId");
member.MemberTypeId = jsonObject.optInt("MemberTypeId");
member.FirmTypeId = jsonObject.optInt("FirmTypeId");
member.IsRegistered = jsonObject.optInt("IsRegistered");
member.MemberUserName = jsonObject.optString("MemberUserName");
member.MemberName = jsonObject.optString("MemberName");
member.UpdateDate = jsonObject.optString("UpdateDate");
member.LastLoginDate = jsonObject.optString("LastLoginDate");
member.FirmName = jsonObject.optString("FirmName");
member.FirmOfficialName = jsonObject.optString("FirmOfficialName");
member.FirmRowGuid = jsonObject.optString("FirmRowGuid");
member.MemberRowGuid = jsonObject.optString("MemberRowGuid");
member.AuthToken = jsonObject.optString("AuthToken");
}
答案 1 :(得分:0)
{
"MemberList": [{
"MemberId": 1,
"FirmId": 1,
"MemberTypeId": 1,
"MemberUserName": "test@example.com",
"MemberName": "sth",
"UpdateDate": "\/Date(1421840040000)\/",
"LastLoginDate": "\/Date(1454995980000)\/",
"FirmTypeId": 1,
"FirmName": "Firm",
"FirmOfficialName": "Firm",
"FirmRowGuid": "sth",
"MemberRowGuid": "sth",
"AuthToken": "sth",
"IsRegistered": 1
}],
"MemberPageList": null,
"FirmModuleList": null,
"Status": true,
"StatusCode": "OK"
}
因为你可以看到我们作为一个对象得到的顶级根数据,在这个对象中我们得到&#34; MemberList&#34;的数组,所以你需要先获取对象然后在其中获取数组。
并执行此操作以将响应转换为String
HttpResponse response=client.execute(post);
String obj= EntityUtils.toString(response.getEntity());
然后根据需要使用它。
希望你能得到这个。