0
我在想,如果將某些JSON快速解釋爲Java數組,像這樣或者如果最好使用類似Jackson的東西那麼快?快速解析Java中與數組混合的深json對象
比方說,我解析一個JSON對象是這樣的:
{ "item1": "item",
"roles": [
{ "roleName": "NormalUser" }
],
"type": "worker"
}
然後,我在這裏的一些Java代碼(響應變量=從RestTemplate ResponseEntity):
logger.info("parsing role...");
HashMap<String, Object> jsonBody = response.getBody();
if(jsonBody.containsKey("roles")){
@SuppressWarnings("unchecked")
List<HashMap<String, Object>> roles = (List<HashMap<String, Object>>)jsonBody.get("roles");
logger.info("Cast Hashmap, now interpreting...");
for (HashMap<String, Object> role : roles) {
logger.info("Found role: " + role.toString());
String assignedRole = "";
if(role.containsKey("role")){
assignedRole = (String) role.get("roleName");
};
this.roleAssignments.add(assignedRole);
logger.info("Assigning role: {}", assignedRole);
}
}
gson或jackson比解析更好 –
實體entity = gson.fromJson(jsonInString,Entity.class); –