为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

拉丁的传说 2023-08-09 17:27:05
我知道使用 Jackson 对象映射器包将 JSON 字符串转换为 JAVA 对象。假设我有像下面提到的高级 Json 字符串。是否可以将其转换为 Java 对象?{    "a": 123123,    "b":true,    "cList":[{        "c1": "valuec1",        "c2": "valuec2",        "c3": "valuec3"        },        {        "c1": "valuec4",        "c2": "valuec5",        "c3": "valuec6"        }]}
查看完整描述

3 回答

?
ABOUTYOU

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;

}

}


查看完整回答
反对 回复 2023-08-09
?
尚方宝剑之说

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;

    }


}


查看完整回答
反对 回复 2023-08-09
?
隔江千里

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);

}

}


查看完整回答
反对 回复 2023-08-09
  • 3 回答
  • 0 关注
  • 130 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信