繁体   English   中英

想要使用arraylist将json字符串反序列化为java对象

[英]want to deserialize json string into java object with arraylist

java对象 :MyObject具有AnotherObject1的列表,而AnotherObject1也具有AnotherObject2的列表

class MyObject{

private String                  status;

private String                  message;

private List<AnotherObject1>            data;
public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<AnotherObject1> getData() {
        return data;
    }

    public void setData(List<AnotherObject1> data) {
        this.data = data;
        }
}

Class AnotherObject1{
private Integer                 group_id;

 private List<AnotherObject2>           anotherList;
public Integer getGroup_id() {
   return group_id;
}

public void setGroup_id(Integer group_id) {
   this.group_id = group_id;
}

public List<AnotherObject2> getAnotherList() {
  return smsList;
}

public void setAnotherList(List<AnotherObject2> anotherList) {
   this.anotherList = anotherList;
}

}

class AnotherObject2{
  private String                    customid;

    private String                  customid1   ;

    private Long                    mobile;

    private String                  status;

    private String                  country;

    public String getCustomid() {
        return customid;
    }

    public void setCustomid(String customid) {
        this.customid = customid;
    }

    public String getCustomid1() {
        return customid1;
    }

    public void setCustomid1(String customid1) {
        this.customid1 = customid1;
    }

    public Long getMobile() {
        return mobile;
    }

    public void setMobile(Long mobile) {
        this.mobile = mobile;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }


}

JSON字符串 :这是我要使用对象映射器制作Java对象的json字符串

String response="{\"status\":\"OK\",\"data\":{\"group_id\":39545922,\"0\":{\"id\":\"39545922-1\",\"customid\":\"\",\"customid1\":\"\",\"customid2\":\"\",\"mobile\":\"910123456789\",\"status\":\"XYZ\",\"country\":\"IN\"}},\"message\":\"WE R Happy.\"}"

ObjectMapper代码 //将字符串转换为响应对象

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
objectMapper.readValue(responseBody, MyObject.class);

例外 :这是例外

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: {"status":"OK","data":{"group_id":39545922,"0":{"id":"39545922-1","customid":"","customid1":"","customid2":"","mobile":"910123456789","status":"GOOD","country":"IN"}},"message":"We R happy."}; line: 1, column: 15] (through reference chain: MyObject["data"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:854)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:850)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:292)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:227)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:256)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3702)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2714)
    at abc.disp(RestClientImpl.java:210)
    at abc.disp(RestClientImpl.java:105)
    at Application.<init>(Application.java:42)
    at Application.main(Application.java:45)

请指导我如何使其成为可能。

您的代码本身不起作用...

private List<AnotherObject1> data; // Your class member is list of AnotherObject1

在它下面用作getter和setter中的SMSDTO列表

public List<SMSDTO> getData() {
    return data;
}

问题很简单:您声称data应该成为Java List 这就要求它的JSON输入应该是JSON Array。 但是,JSON却是一个JSON对象。

因此,您要么需要更改POJO定义,以期望与JSON对象兼容(POJO或java.util.Map ); 或JSON包含data数组。

首先,正如Naveen Ramawat所说的那样,您的代码不可编译。

  • 在类AnotherObject1中,getSmsList应该采用AnotherObject2,而setSmsList应该也采用AnotherObject2作为参数。
  • 在类MyObject中,setData和getData应该使用AnotherObject1作为参数

其次,您的JSON字符串无效,应该是这样的:

{"status":"OK","data":[{"group_id":39545922,"smsList":[{"customid":"39545922-1","customid1":"","mobile":913456789,"status":"XYZ","country":"XYZ"}]}]}

这是我使用的代码:

MyObject.java:

    import java.util.List;

    class MyObject {

        private String status;

        private String message;

        private List<AnotherObject1> data;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public List<AnotherObject1> getData() {
            return data;
        }

        public void setData(List<AnotherObject1> data) {
            this.data = data;
        }
    }

AnotherObject1.java:

    import java.util.List;

    public class AnotherObject1 {
        private Integer group_id;

        private List<AnotherObject2> smsList;

        public Integer getGroup_id() {
            return group_id;
        }

        public void setGroup_id(Integer group_id) {
            this.group_id = group_id;
        }

        public List<AnotherObject2> getSmsList() {
            return smsList;
        }

        public void setSmsList(List<AnotherObject2> smsList) {
            this.smsList = smsList;
        }
    }

AnotherObject2.java:

    public class AnotherObject2 {

        private String customid;

        private String customid1;

        private Long mobile;

        private String status;

        private String country;

        public String getCustomid() {
            return customid;
        }

        public void setCustomid(String customid) {
            this.customid = customid;
        }

        public String getCustomid1() {
            return customid1;
        }

        public void setCustomid1(String customid1) {
            this.customid1 = customid1;
        }

        public Long getMobile() {
            return mobile;
        }

        public void setMobile(Long mobile) {
            this.mobile = mobile;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

    }

要获取JSON字符串:

    import org.json.JSONObject;
    import org.json.XML;

    import com.google.gson.Gson;

    MyObject myObj = new MyObject();
    ArrayList<AnotherObject2> smsList = new ArrayList<AnotherObject2>();
    ArrayList<AnotherObject1> data = new ArrayList<AnotherObject1>();
    AnotherObject1 ao1 = new AnotherObject1();
    ao1.setGroup_id(39545922);
    ao1.setSmsList(smsList);
    AnotherObject2 sms = new AnotherObject2();
    sms.setCountry("XYZ");
    sms.setCustomid("39545922-1");
    sms.setCustomid1("");
    sms.setMobile((long) 913456789);
    sms.setStatus("XYZ");
    smsList.add(sms);
    ao1.setSmsList(smsList);
    data.add(ao1);
    myObj.setStatus("OK");
    myObj.setData(data);
    // Build a JSON string to display
    Gson gson = new Gson();
    String jsonString = gson.toJson(myObj);
    System.out.println(jsonString);
    // Get an object from a JSON string
    MyObject myObject2 = gson.fromJson(jsonString, MyObject.class);
    // Display the new object
    System.out.println(gson.toJson(myObject2));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM