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

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


{

    "a": 123123,

    "b":true,

    "cList":[{

        "c1": "valuec1",

        "c2": "valuec2",

        "c3": "valuec3"

        },

        {

        "c1": "valuec4",

        "c2": "valuec5",

        "c3": "valuec6"

        }]

}


拉丁的传说
浏览 131回答 3
3回答

ABOUTYOU

public class Codebeautify {private float a;private boolean b;ArrayList < Object > cList = new ArrayList < Object > ();// Getter Methods&nbsp;public float getA() {&nbsp; &nbsp; return a;}public boolean getB() {&nbsp; &nbsp; return b;}// Setter Methods&nbsp;public void setA(float a) {&nbsp; &nbsp; this.a = a;}public void setB(boolean b) {&nbsp; &nbsp; this.b = b;}}

尚方宝剑之说

class Example {&nbsp; &nbsp; public int a;&nbsp; &nbsp; public boolean b;&nbsp; &nbsp; public List<Exmaple1> cList;&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "Example [a=" + a + ", flag=" + b + ", e1=" + cList + "]";&nbsp; &nbsp; }}&nbsp; &nbsp; class Exmaple1 {&nbsp; &nbsp; public String c1;&nbsp; &nbsp; public String c2;&nbsp; &nbsp; public String c3;&nbsp; &nbsp; public String getC1() {&nbsp; &nbsp; &nbsp; &nbsp; return c1;&nbsp; &nbsp; }&nbsp; &nbsp; public void setC1(String c1) {&nbsp; &nbsp; &nbsp; &nbsp; this.c1 = c1;&nbsp; &nbsp; }&nbsp; &nbsp; public String getC2() {&nbsp; &nbsp; &nbsp; &nbsp; return c2;&nbsp; &nbsp; }&nbsp; &nbsp; public void setC2(String c2) {&nbsp; &nbsp; &nbsp; &nbsp; this.c2 = c2;&nbsp; &nbsp; }&nbsp; &nbsp; public String getC3() {&nbsp; &nbsp; &nbsp; &nbsp; return c3;&nbsp; &nbsp; }&nbsp; &nbsp; public void setC3(String c3) {&nbsp; &nbsp; &nbsp; &nbsp; this.c3 = c3;&nbsp; &nbsp; }}

隔江千里

class B {public String c1;public String c2;public String c3;public B() {}public B(String c1, String c2, String c3) {&nbsp; &nbsp; this.c1 = c1;&nbsp; &nbsp; this.c2 = c2;&nbsp; &nbsp; this.c3 = c3;}@Overridepublic String toString() {&nbsp; &nbsp; return "{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " c1 :'" + c1 + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ", c2 :'" + c2 + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ", c3 :'" + c3 + '\'' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}';}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) {&nbsp; &nbsp; this.a = a;&nbsp; &nbsp; this.b = b;&nbsp; &nbsp; this.cList = cList;}@Overridepublic String toString() {&nbsp; &nbsp; return "{" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " a :" + a +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ", b :" + b +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ", cList :" + cList +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '}';}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) {&nbsp; &nbsp; List<B> cList = new ArrayList<>();&nbsp; &nbsp; cList.add(new B("x1", "x2", "x3"));&nbsp; &nbsp; cList.add(new B("y1", "y2", "y3"));&nbsp; &nbsp; C obj = new C(123, true, cList);&nbsp; &nbsp; ObjectMapper objectMapper = new ObjectMapper();// obj --> json&nbsp; &nbsp; String json = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; json = objectMapper.writeValueAsString(obj);&nbsp; &nbsp; } catch (JsonProcessingException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(json == null ? "error" : json);// json1 --> obj1&nbsp; &nbsp; //String json1 = "{\"a\":123,\"b\":true,\"cList\":[{\"c1\":\"x1\",\"c2\":\"x2\",\"c3\":\"x3\"},{\"c1\":\"y1\",\"c2\":\"y2\",\"c3\":\"y3\"}]}";&nbsp; &nbsp; String json1 = json;&nbsp; &nbsp; C obj1 = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; obj1 = objectMapper.readValue(json1, C.class);&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(obj1 == null ? "error" : obj1);}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java