简体   繁体   English

无法将JsonObject转换为JsonArray

[英]Unable to Convert JsonObject to JsonArray

I'm having problem while converting jsonObject to jsonArray 将jsonObject转换为jsonArray时遇到问题

This is my Json string: 这是我的Json字符串:

{"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. 我已经使用在线工具验证了此json,它是有效的。

But when I try to it into JSONArray it give me this exception: 但是,当我尝试将其放入JSONArray时,会出现以下异常:

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 org.json.JSONException:值[{“ 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}],位于java.lang类型的GetAllEmployeeResult。字符串不能转换为JSONArray

Following is my code: 以下是我的代码:

JSONObject jObject = new JSONObject(buffer.toString());
JSONArray jArray = jObject.getJSONArray("GetAllEmployeeResult");

*buffer hold JSON string *缓冲区保存JSON字符串

Thanks 谢谢

Because the value of GetAllEmployeeResult key is a String , not a JSONArray . 因为GetAllEmployeeResult键的值是String ,而不是JSONArray There are double quotes surrounding the JSONArray . 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. 使用Java API进行JSON处理,将JsonObject转换为JsonArray,也可以在下面尝试。

JsonArray array = Json.createArrayBuilder()
    .add(jObject)
    .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM