Parsing JSON to POJO using GSON

2019-01-28 20:55发布

My JSON looks like follow :

"message_defaults": {
      "LabResultsRequestDefaultMessage": {
        "MsgTypeId": 8,
        "StaffId": 122,
        "StaffName": "Willis,Sam",
        "FirstName": "Sam",
        "MI": "D",
        "LastName": "Willis",
        "DefaultMessage": "Lab Department"
      },
      "ReferralRequestDefaultMessage": {
        "MsgTypeId": 6,
        "StaffId": 122,
        "StaffName": "Willis,Sam",
        "FirstName": "Sam",
        "MI": "D",
        "LastName": "Willis",
        "DefaultMessage": "Physican"
      },
      "MessageComposeDefaultMessage": {
        "MsgTypeId": 1,
        "StaffId": 122,
        "StaffName": "Willis,Sam",
        "FirstName": "Sam",
        "MI": "D",
        "LastName": "Willis",
        "DefaultMessage": "Office Manager/Willis Sam"
      }
}

Now each elemet in message_defaluts is having same structure (Say one POJO called MessageDefault.java). So how can I get all message defaults as a List<MessageDefault> ??

I am using gson for parsing. Also I can't change JSON response.

EDIT ::

MessageDefault.java

public class MessageDefault{
    private String defaultMessage;
    private String firstName;
    private String lastName;
    private String mI;
    private int msgTypeId;
    private int staffId;
    private String staffName;

    public String getDefaultMessage(){
        return this.defaultMessage;
    }
    public void setDefaultMessage(String defaultMessage){
        this.defaultMessage = defaultMessage;
    }
    public String getFirstName(){
        return this.firstName;
    }
    public void setFirstName(String firstName){
        this.firstName = firstName;
    }
    public String getLastName(){
        return this.lastName;
    }
    public void setLastName(String lastName){
        this.lastName = lastName;
    }
    public String getMI(){
        return this.mI;
    }
    public void setMI(String mI){
        this.mI = mI;
    }
    public int getMsgTypeId(){
        return this.msgTypeId;
    }
    public void setMsgTypeId(int msgTypeId){
        this.msgTypeId = msgTypeId;
    }
    public int getStaffId(){
        return this.staffId;
    }
    public void setStaffId(int staffId){
        this.staffId = staffId;
    }
    public String getStaffName(){
        return this.staffName;
    }
    public void setStaffName(String staffName){
        this.staffName = staffName;
    }
}

Its is just structure of all message_defualts . But In JSON, each child of message_defualts is having different name like LabResultsRequestDefaultMessage, ReferralRequestDefaultMessage etc which are being reflected in POJO MessageDefault.java. But from JSON I want List of them.

Thanks.

2条回答
啃猪蹄的小仙女
2楼-- · 2019-01-28 21:48

You can do the following without changing your json structure.

Type mapType = new TypeToken<Map<String, MessageDefault>>() {}.getType();
Map<String, MessageDefault> messagesMap = gson.fromJson(json, mapType);
List<MessageDefault> messages = new ArrayList<MessageDefault>(messagesMap.values());
查看更多
孤傲高冷的网名
3楼-- · 2019-01-28 21:49

If you want to get List from json first you have to correct your json. List must be in '[ ]'.

"message_defaults": [{
      "LabResultsRequestDefaultMessage": {
        "MsgTypeId": 8,
        "StaffId": 122,
        "StaffName": "Willis,Sam",
        "FirstName": "Sam",
        "MI": "D",
        "LastName": "Willis",
        "DefaultMessage": "Lab Department"
      },
      "ReferralRequestDefaultMessage": {
        "MsgTypeId": 6,
        "StaffId": 122,
        "StaffName": "Willis,Sam",
        "FirstName": "Sam",
        "MI": "D",
        "LastName": "Willis",
        "DefaultMessage": "Physican"
      },
      "MessageComposeDefaultMessage": {
        "MsgTypeId": 1,
        "StaffId": 122,
        "StaffName": "Willis,Sam",
        "FirstName": "Sam",
        "MI": "D",
        "LastName": "Willis",
        "DefaultMessage": "Office Manager/Willis Sam"
      }
}]

EDIT : try this for converting to list

Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);

Follow this link

查看更多
登录 后发表回答