[英]How to handle multiple json fields in gson
I want to parse a json to an object which gives me details of every entity attached to a bank. 我想将json解析为一个对象,该对象为我提供了连接到银行的每个实体的详细信息。
My json looks like : 我的json看起来像:
{
"href" : "abc",
"configurations" :
[
{
"type" : "bank-customer",
"properties" : {
"cust-name" : "foo",
"acc-no" : "12345"
}
},
{
"type" : "bank-employee",
"properties" : {
"empl-name" : "foo",
"empl-no" : "12345"
}
}
]
}
The properties for various entity "type" is different. 各种实体“类型”的属性是不同的。
Creating a pojo for this is the challenge. 为此创建一个pojo是挑战。 My properties.java will have to include all properties irrespective of the type of the property :
我的properties.java将必须包括所有属性,而与属性的类型无关:
public class Configurations {
@SerializedName("type")
@Expose
private String entityType;
@SerializedName("properties")
@Expose
private Properties properties;
}
public class Properties {
@SerializedName("cust-name")
@Expose
private String custName;
@SerializedName("empl-no")
@Expose
private String emplNo;
@SerializedName("empl-name")
@Expose
private String emplName;
@SerializedName("acc-no")
@Expose
private String accNo;
}
This is painful when I have a lot of entity types and property per entity type. 当我每个实体类型都有很多实体类型和属性时,这很痛苦。 Is there any other way I can parse this json into different property objects for different entity types?
我还有其他方法可以将此json解析为不同实体类型的不同属性对象吗? I am using gson to parse the JSON
我正在使用gson解析JSON
Note : I can't make any changes to the json itself. 注意:我无法对json本身进行任何更改。
You probably need to create your own deserializer, take a look at this example . 您可能需要创建自己的解串器,请看以下示例 。
A possible implementation 可能的实现
Deserializer: 解串器:
package it.test.gson;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
public class BankDeserializer implements JsonDeserializer<Bank> {
@Override
public Bank deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
final JsonObject jsonObject = json.getAsJsonObject();
final JsonElement jsonHref = jsonObject.get("href");
final String href = jsonHref.getAsString();
final JsonArray jsonConfigurationsArray = jsonObject.get("configurations").getAsJsonArray();
final String[] configurations = new String[jsonConfigurationsArray.size()];
List<IPerson> persons = new ArrayList<>();
for (int i = 0; i < configurations.length; i++) {
final JsonElement jsonConfiguration = jsonConfigurationsArray.get(i);
final JsonObject configJsonObject = jsonConfiguration.getAsJsonObject();
final String type = configJsonObject.get("type").getAsString();
final JsonObject propertiesJsonObject = configJsonObject.get("properties").getAsJsonObject();
IPerson iPerson = null;
if (type.equals("bank-customer")) {
iPerson = new Customer();
final String name = propertiesJsonObject.get("cust-name").getAsString();
final String no = propertiesJsonObject.get("acc-no").getAsString();
iPerson.setName(name);
iPerson.setNo(no);
} else if (type.equals("bank-employee")) {
iPerson = new Employee();
final String name = propertiesJsonObject.get("empl-name").getAsString();
final String no = propertiesJsonObject.get("empl-no").getAsString();
iPerson.setName(name);
iPerson.setNo(no);
}
persons.add(iPerson);
}
final Bank bank = new Bank();
bank.setHref(href);
bank.setConfiguration(persons.toArray(new IPerson[0]));
return bank;
}
}
POJOs: POJO:
Bank 银行
package it.test.gson;
public class Bank {
private String href;
private IPerson[] configuration;
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
public IPerson[] getConfiguration() {
return configuration;
}
public void setConfiguration(IPerson[] configuration) {
this.configuration = configuration;
}
}
Interface for persons 人机界面
package it.test.gson;
public interface IPerson {
public String getName();
public void setName(String name);
public String getNo();
public void setNo(String no);
}
Implementation of persons, your employee or customer 实施人员,您的员工或客户
package it.test.gson;
public class Customer implements IPerson {
private String name;
private String no;
@Override
public void setName(String name) {
this.name = name;
}
@Override
public void setNo(String no) {
this.no = no;
}
@Override
public String getName() {
return name;
}
@Override
public String getNo() {
return no;
}
}
package it.test.gson;
public class Employee implements IPerson {
private String name;
private String no;
public void setName(String name) {
this.name = name;
}
public void setNo(String no) {
this.no = no;
}
@Override
public String getName() {
return name;
}
@Override
public String getNo() {
return no;
}
}
and the Main class to test it 和主类来测试它
package it.test.gson;
import java.io.InputStreamReader;
import java.io.Reader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class Main {
public static void main(String[] args) throws Exception {
// Configure Gson
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Bank.class, new BankDeserializer());
Gson gson = gsonBuilder.create();
// The JSON data
try (Reader reader = new InputStreamReader(Main.class.getResourceAsStream("/part1/sample.json"), "UTF-8")) {
// Parse JSON to Java
Bank bank = gson.fromJson(reader, Bank.class);
System.out.println(bank.getHref());
//...
}
}
}
I hope it helps. 希望对您有所帮助。
I completely agree with mauros answer. 我完全同意毛罗的回答。
But you can also create a interface hierarchy and implement Instance Creator . 但是,您也可以创建接口层次结构并实现Instance Creator 。
Its can be easily solved by using alternate
keyword from @SerializedName annotation. 使用@SerializedName批注中的
alternate
关键字可以轻松解决该问题。
public class Properties {
@SerializedName(value="cust-name", alternate={"empl-name", "user-name"})
@Expose
private String name;
@SerializedName("acc-no", alternate={"empl-no", "user-id"})
@Expose
private String id;
//setter and getters
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.