From below json object , I want to return(/or add the keys to a Set) whose values are true.
{
"create": true,
"read": false,
"update": true,
"delete": false
}
I want an output like below
[ create, update ]
Could some one help me with this?
Update :
I was trying as shown below to capture all the values that are true , but I think my Java 8 filter criteria is wrong.
Map<String, Object> mapping = new ObjectMapper().readValue(permissionStr, HashMap.class);
mySet = mapping.entrySet().stream().filter(x->x.getValue().equals(Boolean.TRUE)).map(Map.Entry::getKey)
.collect(Collectors.toSet());
CodePudding user response:
Set each field to default to false in the Java class and annotate the class with @JsonInclude(JsonInclude.Include.NON_DEFAULT). This checks the instance of the class you created with a default instance created with a no args constructor and only sends the values that have changed, in your case any value that is true.
When parsing the JSON output, you can then check which keys are in the object.
CodePudding user response:
List<String> list;
String VariableName = JSONObjectName.getString("Create");
if(VariableName.equals("true")){
list.add(VariableName);
}