高级json字符串到java对象的处理和转换

高级json字符串到java对象的处理和转换,java,json,jackson-databind,Java,Json,Jackson Databind,我知道使用Jackson object mapper包将JSON字符串转换为JAVA对象。 假设我有下面提到的高级Json字符串。是否可以将其转换为Java对象 { "a": 123123, "b":true, "cList":[{ "c1": "valuec1", "c2": "valuec2", "c3": "valuec3" }, { "c1": "valuec4",

我知道使用Jackson object mapper包将JSON字符串转换为JAVA对象。 假设我有下面提到的高级Json字符串。是否可以将其转换为Java对象

{
    "a": 123123,
    "b":true,
    "cList":[{
        "c1": "valuec1",
        "c2": "valuec2",
        "c3": "valuec3"
        },
        {
        "c1": "valuec4",
        "c2": "valuec5",
        "c3": "valuec6"
        }]
}

是的,为精确映射创建POJO,您可以使用相同的对象映射来完成。。。克莱斯特怎么样?是否需要声明为列表???仅供参考,这可能会有所帮助
public class Codebeautify {
private float a;
private boolean b;
ArrayList < Object > cList = new ArrayList < Object > ();


// Getter Methods 

public float getA() {
    return a;
}

public boolean getB() {
    return b;
}

// Setter Methods 

public void setA(float a) {
    this.a = a;
}

public void setB(boolean b) {
    this.b = b;
}
}
class Example {

    public int a;
    public boolean b;
    public List<Exmaple1> cList;
    @Override
    public String toString() {
        return "Example [a=" + a + ", flag=" + b + ", e1=" + cList + "]";
    }

}

    class Exmaple1 {
    public String c1;
    public String c2;
    public String c3;
    public String getC1() {
        return c1;
    }
    public void setC1(String c1) {
        this.c1 = c1;
    }
    public String getC2() {
        return c2;
    }
    public void setC2(String c2) {
        this.c2 = c2;
    }
    public String getC3() {
        return c3;
    }
    public void setC3(String c3) {
        this.c3 = c3;
    }

}
class B {
public String c1;
public String c2;
public String c3;

public B() {}
public B(String c1, String c2, String c3) {
    this.c1 = c1;
    this.c2 = c2;
    this.c3 = c3;
}

@Override
public String toString() {
    return "{" +
            " c1 :'" + c1 + '\'' +
            ", c2 :'" + c2 + '\'' +
            ", c3 :'" + c3 + '\'' +
            '}';
}
public String getC1() {return c1;}
public void setC1(String c1) {this.c1 = c1;}
public String getC2() {return c2;}
public void setC2(String c2) {this.c2 = c2;}
public String getC3() {return c3;}
public void setC3(String c3) {this.c3 = c3;}
}

public class C {
public int a;
public boolean b;
public List<B> cList;

public C() {}
public C(int a, boolean b, List<B> cList) {
    this.a = a;
    this.b = b;
    this.cList = cList;
}

@Override
public String toString() {
    return "{" +
            " a :" + a +
            ", b :" + b +
            ", cList :" + cList +
            '}';
}
public int getA() {return a;}
public void setA(int a) {this.a = a;}
public boolean isB() {return b;}
public void setB(boolean b) {this.b = b;}
public List<B> getcList() {return cList;}
public void setcList(List<B> cList) {this.cList = cList;}

public static void main(String[] args) {
    List<B> cList = new ArrayList<>();
    cList.add(new B("x1", "x2", "x3"));
    cList.add(new B("y1", "y2", "y3"));
    C obj = new C(123, true, cList);
    ObjectMapper objectMapper = new ObjectMapper();

// obj --> json
    String json = null;
    try {
        json = objectMapper.writeValueAsString(obj);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    System.out.println(json == null ? "error" : json);

// json1 --> obj1
    //String json1 = "{\"a\":123,\"b\":true,\"cList\":[{\"c1\":\"x1\",\"c2\":\"x2\",\"c3\":\"x3\"},{\"c1\":\"y1\",\"c2\":\"y2\",\"c3\":\"y3\"}]}";
    String json1 = json;
    C obj1 = null;
    try {
        obj1 = objectMapper.readValue(json1, C.class);
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(obj1 == null ? "error" : obj1);
}
}