这是对我之前的问题Extract specific value from complex JSON in java的扩展 。我得到了解决方案,但我想尝试从带有 bean 类的 json 字符串中提取 3 个值。对于给定的 json 结构。json结构在链接中。
我正在使用以下代码调用杰克逊:
mainbean obj = mapper.readValue(file, mainbean.class);
System.out.println(obj.clientPayload.e.h.getAk());
我已经为 mainbean 类的整个层次结构生成了 POJO:
public class mainbean {
public Cpld cp;
public Cpld getCp() {
return Cp;
}
public void setCp(Cpld cp) {
this.cp= cp;
}
public SPL getSp() {
return sp;
}
public void setSp(SPL sp) {
this.sp= sp;
}
private SPL sp;
@Override
public String toString() {
return "mainbean [cp=" + cp+ "]";
}
}
其次是CPLD类
public class Cpld {
public E e;
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
public String toString(){
return "E string" + e;
}
}
然后e课
public class E {
public H h;
public H getH() {
return h;
}
public void setH(H h) {
this.h = h;
}
public C c;
public B b;
public TT tt;
public C getC() {
return c;
}
public void setC(C c) {
this.c = c;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
public TT getTt() {
return tt;
}
public void setTt(TT tt) {
this.tt = tt;
}
}
等等......我遇到了一个例外
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class JsonPath.Cpld] from JSON String; no single-String constructor/factory method (through reference chain: JsonPath.mainbean["clientPayload"])
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:419)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:295)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromString(BeanDeserializer.java:405)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:117)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:336)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:89)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:290)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:112)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2577)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1764)
谁能在这里为我指明正确的方向...在此先感谢您的帮助
更新:Json 文件:从 java 中的复杂 JSON 中提取特定值