My JSON
looks like follow :
我的JSON看起来如下:
"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>
??
现在message_defaluts中的每个元素都具有相同的结构(比如一个名为MessageDefault.java的POJO)。那么,如何将所有消息默认设置为列表
I am using gson for parsing. Also I can't change JSON response.
我正在使用gson进行解析。我也不能改变JSON响应。
EDIT ::
编辑::
MessageDefault.java
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.
它只是所有message_defualts的结构。但是在JSON中,message_defualts的每个子节点都有不同的名称,比如LabResultsRequestDefaultMessage、ReferralRequestDefaultMessage等等,这些名称在POJO MessageDefault.java中反映出来。但从JSON中,我需要它们的列表。
Thanks.
谢谢。
2 个解决方案
#1
0
You can do the following without changing your json structure.
您可以在不改变json结构的情况下执行以下操作。
Type mapType = new TypeToken<Map<String, MessageDefault>>() {}.getType();
Map<String, MessageDefault> messagesMap = gson.fromJson(json, mapType);
List<MessageDefault> messages = new ArrayList<MessageDefault>(messagesMap.values());
#2
1
If you want to get List from json first you have to correct your json. List must be in '[ ]'.
如果您想首先从json中获取列表,您必须纠正您的json。列表必须在“[]”中。
"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);
按照这个链接
#1
0
You can do the following without changing your json structure.
您可以在不改变json结构的情况下执行以下操作。
Type mapType = new TypeToken<Map<String, MessageDefault>>() {}.getType();
Map<String, MessageDefault> messagesMap = gson.fromJson(json, mapType);
List<MessageDefault> messages = new ArrayList<MessageDefault>(messagesMap.values());
#2
1
If you want to get List from json first you have to correct your json. List must be in '[ ]'.
如果您想首先从json中获取列表,您必须纠正您的json。列表必须在“[]”中。
"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);
按照这个链接