[英]Parsing json variable objects with same name using gson
Hi I want to parse the json 嗨,我想解析json
{
"entityType":"Account",
"values":[
{
"name":"accountType",
"type":"lookup",
"typeinfo":{"entity":"lov", "filter":"LOV_ACCOUNT_TYPE"}
},
{
"name":"irstSubType",
"type":"lookup",
"typeinfo":{"entity":"lov", "filter":"LOV_ACCOUNT_TYPE"}
},
{
"name":"accountStatus",
"type":"lookup",
"typeinfo":{"entity":"lov", "filter":"LOV_ACCOUNT_STATUS"}
},
{
"name":"name",
"type":"string",
"typeinfo":{"width":80}
},
{
"name":"updated",
"type":"datetime",
"typeinfo":{"format":"dd/MM/yyyy hh:MM:ss"}
}
]
}
So here typeinfo object implementation is changing.. any suggessions how does Gson library accomodate this? 所以这里的typeinfo对象实现正在改变..任何建议Gson库如何适应这一点? Interface does not work.
界面不起作用。
Value 值
public class Value {
private String name;
private String type;
private TypeInfo typeinfo;
public String getName() {
return name;
}
public String getType() {
return type;
}
public TypeInfo getTypeinfo() {
return typeinfo;
}
}
MyGson 麦格森
public class MyGson {
private String entityType;
private List<Value> values;
public String getEntityType() {
return entityType;
}
public List<Value> getValues() {
return values;
}
}
TypeInfo 类型信息
public class TypeInfo {
private String format;
private String entry;
private String filter;
public String getFormat() {
return format;
}
public String getEntry() {
return entry;
}
public String getFilter() {
return filter;
}
}
Launcher 发射器
Gson gson = new Gson();
MyGson myGson = gson.fromJson(str, MyGson.class);
List<Value> values = myGson.getValues();
for(Value value : values){
str = value.getTypeinfo().getFormat();
printValues("format", value.getTypeinfo().getFormat());
printValues("entry", value.getTypeinfo().getEntry());
printValues("filter", value.getTypeinfo().getFilter());
//....
}
}
private static void printValues(String key, String data){
if(data != null){
System.out.println(key + ": " + data);
}
}
Output: 输出:
filter: LOV_ACCOUNT_TYPE
filter: LOV_ACCOUNT_TYPE
filter: LOV_ACCOUNT_STATUS
format: dd/MM/yyyy hh:MM:ss
Despite the other answers, it is not a good idea to have a field for each of the possible variables in typeinfo, as two fields could have same name but different types. 尽管有其他答案,但在typeinfo中为每个可能的变量都具有一个字段并不是一个好主意,因为两个字段可能具有相同的名称但类型不同。
What you really want to do, is to turn the data from typeinfo into a Map<String, Object>. 您真正想要做的是将数据从typeinfo转换为Map <String,Object>。
This is difficult in Gson, but it is possible using a deserializer - see Kevin's answer for an example of this: https://stackoverflow.com/a/4799594/1660737 这在Gson中很难,但是可以使用解串器-有关示例,请参见Kevin的答案: https : //stackoverflow.com/a/4799594/1660737
String s = "...json string ..";
JSONParser jsonparser = new JSONParser();
JSONObject jsonObject = (JSONObject)jsonparser.parse(s);
JSONArray values = (JSONArray) jsonObject.get("values");
for(int i=0; i< values.size(); i++){
JSONObject value = (JSONObject)values.get(i);
JSONObject typeInfo = (JSONObject)value.get("typeinfo");
Iterator it = typeInfo.keySet().iterator();
while(it.hasNext()){
Object key = it.next();
System.out.println("KEY:" + key + " VALUE:" + typeInfo.get(key));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.