Google 的 cAdvisor API 提供如下 JSON 输出:
{
/system.slice/docker-13b18253fa70d837e9707a1c28e45a3573e82751f964b66d7c4cbc2256abc266.scope: {},
/system.slice/docker-747f797d19931b4ef33cda0c519f935b592a0b828d16b8cafc350568ab2c1d28.scope: {},
/system.slice/docker-bf947bfabf61cd5168bd599162cf5f5c2ea2350eece1ded018faebf598f7ee5b.scope: {},
/system.slice/docker-e8e02d508400438603151dd462ef036d59fada8239f66be8e64813880b59a77d.scope: {
name: "/system.slice/docker-e8e02d508400438603151dd462ef036d59fada8239f66be8e64813880b59a77d.scope",
aliases: [...],
namespace: "docker",
spec: {...},
stats: [...]
}
}
我会将其描述为 4 个相同类型的 JSON 对象,它们具有变量/匿名名称,保存在一个匿名对象中。
我的第一个想法就是做类似的事情mapper.readValue(response, Containers.class)
,其中:
public class Containers extends BaseJsonObject {
@JsonProperty
public List<Container> containerList;
}
和
public class Container extends BaseJsonObject {
@JsonProperty
private String name;
@JsonProperty
public String[] aliases;
@JsonProperty
private String namespace;
@JsonProperty
private String spec;
@JsonProperty
public Stats[] stats;
}
但是我能想到的所有关于这个的变化都会产生相同的结果:com.xyz.Containers@45c7e403[containerList=<null>]
or com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "/system.slice/docker-13b18253fa70d837e9707a1c28e45a3573e82751f964b66d7c4cbc2256abc266.scope" (class com.xyz.Containers), not marked as ignorable (one known property: "containerList"])
at [Source: java.io.StringReader@3d285d7e; line: 1, column: 97] (through reference chain: com.xyz.Containers["/system.slice/docker-13b18253fa70d837e9707a1c28e45a3573e82751f964b66d7c4cbc2256abc266.scope"])
, with的一些排列ACCEPT_SINGLE_VALUE_AS_ARRAY = false
。
我试过了:
mapper.readValue(response, Container[].class)
mapper.readValue(response, Containers.class)
mapper.readValues(jsonParser, Container.class)
以及以下配置:
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
如何解析具有变量/匿名名称、保存在非数组中的 JSON 对象?这个叫什么?