请为全局对象添加对象
Class ResultJson{
String last_sync_dt;
ArrayList<MeetingInfo> meetings_info;
}
和 MeetingInfo 将是
public class MeetingInfo {
private String date;
private String meeting_id;
private String subject;
private CustomerInfo customerInfo;
public void setDate(String date) {
this.date = date;
}
public void setMeeting_id(String meeting_id) {
this.meeting_id = meeting_id;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setCustomer(CustomerInfo customer) {
customerInfo = customer;
}
}
客户信息类
public class CustomerInfo {
private String customer_id;
private String customer_name;
public void setCustomerId(String customer_id) {
this.customer_id = customer_id;
}
public void setCustomerName(String customer_name) {
this.customer_name = customer_name;
}
}
会议解串器
public class MeetingInfoAutho implements JsonDeserializer<MeetingInfo>{
@Override
public MeetingInfo deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
JsonObject jObject = jsonElement.getAsJsonObject();
MeetingInfo info = new MeetingInfo();
CustomerInfo customer = new CustomerInfo();
customer.setCustomerId(jObject.get("customer_id").getAsString());
customer.setCustomerName(jObject.get("customer_name").getAsString());
info.setDate(jObject.get("date").getAsString());
info.setMeeting_id(jObject.get("meeting_id").getAsString());
info.setSubject(jObject.get("subject").getAsString());
info.setCustomer(customer);
Log.e("info", jObject.toString());
return info;
}
}
最后调用 json 字符串到对象
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(MeetingInfo.class, new MeetingInfoAutho());
Gson gson = gsonBuilder.create();
ResultJson resultObject = gson.fromJson(jsonStr, ResultJson.class);
您应该创建实现 JsonDeserializer 的 MeetingInfoAutho。请查找有关 JsonDeserializer GSON 的一些示例以获取更多信息。
这将给出准确的结果。