I'm having problem while converting jsonObject to jsonArray
This is my Json string:
{"GetAllEmployeeResult":"[{\"firstName\":\"Super\",\"lastName\":\"User\",\"empID\":\"admin\",\"pswd\":\"2031189006230102079245048053200\",\"position\":null,\"contactNo\":null,\"backupCode\":null,\"email\":\"0\",\"hcode\":null,\"empStatus\":\"A\",\"createdBy\":null,\"dateAdded\":null,\"updatedBy\":null,\"dateUpdated\":null,\"profileID\":null,\"locationsID\":null,\"former\":null,\"secContact\":null,\"NoOfAttempts\":null,\"workstation\":null}]"}
I have verified this json with online tools and it is valid.
But when I try to it into JSONArray it give me this exception:
org.json.JSONException: Value [{"firstName":"Super","lastName":"User","empID":"admin","pswd":"056118220216006084031189006230102079245048053200","position":null,"contactNo":null,"backupCode":null,"email":"0","hcode":null,"empStatus":"A","createdBy":null,"dateAdded":null,"updatedBy":null,"dateUpdated":null,"profileID":null,"locationsID":null,"former":null,"secContact":null,"NoOfAttempts":null,"workstation":null}] at GetAllEmployeeResult of type java.lang.String cannot be converted to JSONArray
Following is my code:
JSONObject jObject = new JSONObject(buffer.toString());
JSONArray jArray = jObject.getJSONArray("GetAllEmployeeResult");
*buffer hold JSON string
Thanks
Because the value of GetAllEmployeeResult
key is a String
, not a JSONArray
. There are double quotes surrounding the JSONArray
.
你可以这样改变
JsonArray array = new JsonArray(jObject.getString("GetAllEmployeeResult"));
Using the Java API for JSON Processing,to convert an JsonObject to a JsonArray, you can also try below.
JsonArray array = Json.createArrayBuilder()
.add(jObject)
.build();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.