隔江千里
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;}@Overridepublic 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;}@Overridepublic 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);}}