3 回答
TA贡献1812条经验 获得超5个赞
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;
}
}
TA贡献1788条经验 获得超4个赞
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;
}
}
TA贡献1906条经验 获得超10个赞
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);
}
}
添加回答
举报