如何使用 JAVA 代码检查嵌套 JSON 值是否包含该列


{
"Animals":[
{
"name":"parrot";
},
{
"name":"lion";
"type":"carnivours"
},
{
"name":"cow";
"type":"herbivours"
}
]
}
if(Animals.has("type") && Animals.getString("type")!=null)
{
String type=clusters.get("type").toString(); 
} 
else 
{
String type=null; 
}      

在上面的示例代码中,所有 json 都不包含类型列。 如何检查所有 json 响应是否存在该类型列,如果不是,则仅对该列返回 null。

您可以使用@JsonIgnoreProperties定义和动物类,如下所示。

@JsonIgnoreProperties(ignoreUnknown = true)
public class Animal{
private String name;
private String type;
}

然后,您可以定义另一个类,该类将包含动物列表。然后杰克逊将映射JSON,然后你可以简单地对动物对象使用非空检查。

最新更新