【问题标题】:Json parsing using jackson or Gson使用jackson或Gson解析Json
【发布时间】:2017-06-28 18:01:36
【问题描述】:

如何使用jacksonGson 将相同的 json 对象键解析为 2 个不同的模型类?

这是输入json

 {
      "last_sync_dt": "1486711867749",
      "meetings_info": [
        {
          "date": "2017-01-15",
          "meeting_id": "1",
          "subject": "Product Review with AUDI",
          "customer_id": "32",
          "customer_name": "David"
        }
      ]
    }  

这些是模型类

@JsonIgnoreProperties(ignoreUnknown = true)
Class MeetingInfo{
    @JsonProperty("date")
    private String date;
    @JsonProperty("meeting_id")
    private String meetingId;
    @JsonProperty("subject")
    private String subject;

    CustomerInfo customerinfo; 


//Other fields and getter setter


}

class CustomerInfo{
    @JsonProperty("customer_id")
    private String id;
   @JsonProperty("customer_name")
    private String name;

//Other fields and getter setter
}

【问题讨论】:

  • TypeAdapter ...
  • 您能否再解释一下。我是这个解析库的新手

标签: android jackson2 gson


【解决方案1】:

这是您的代码中使用 gson 的示例。

@JsonIgnoreProperties(ignoreUnknown = true)
Class RootClass {
    @JsonProperty("last_sync_dt")
    private String date;
    @JsonProperty("meetings_info")
    ArrayList<MeetingInfo> meetingInfo;
    // as you are having json array of meeting info in root 


//Other fields and getter setter


}

在你的 MeetingInfo 课堂上

class MeetingInfo{
    @JsonProperty("date")
    private String date;
   @JsonProperty("meeting_id")
    private String meetingId;
   @JsonProperty("subject")
    private String subject;
   @JsonProperty("customer_name")
    private String cName;
   @JsonProperty("customer_id")
    private String cId;

//Other fields and getter setter
} 

最后是你得到 json 响应的地方。

Type type = new TypeToken<RootClass>() {}.getType();
RootClass rootClass = ServerController.gson.fromJson(responseObject.toString(), type);

【讨论】:

  • 您将两个不同的模型类合并为一个。我需要被分开。 MeetingInfo 和 customerInfo
  • 这个模型匹配给定的 JSON。如果您需要转换为已发布的 java 类,请在两个模型之间添加一个 Mapper。这将分离您的“通信数据模型”和您的业务逻辑数据模型
【解决方案2】:

您可以使用此链接使用 jackson 或 Gson 解析 JSON。 它会自动创建你的类。只需将你的 JSON 粘贴到那里。

链接:http://www.jsonschema2pojo.org/

【讨论】:

    【解决方案3】:

    请为全局对象添加对象

    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 的一些示例以获取更多信息。 这将给出准确的结果。

    【讨论】:

    • 您将两个不同的模型类合并为一个。我需要被分开。 MeetingInfo 和 customerInfo
    • 如果你想要这样,那么你必须像这样编辑你的 json 字符串 { "last_sync_dt": "1486711867749", "meetings_info": [ { "date": "2017-01-15", "meeting_id": "1", "subject": "奥迪产品评论", "customer":{ "customer_id": "32", "customer_name": "David" } } ] }
    • 它已经使用了,无法更改。更改 json 会增加工作量。
    • @Meonix 请检查我的编辑。这可以提供您需要的确切对象。
    【解决方案4】:

    这里最好的方法是,您可以从以下 url 为 Gson 或 Jackson 生成模型类,然后您可以使用 Gson 或 Jackson 库直接在模型类中设置 Json 数据。

    生成模型的链接:http://www.jsonschema2pojo.org/

    我正在为您的回复生成模型类。

    import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class Example {
    
    @SerializedName("last_sync_dt")
    @Expose
    private String lastSyncDt;
    @SerializedName("meetings_info")
    @Expose
    private List<MeetingsInfo> meetingsInfo = null;
    
    public String getLastSyncDt() {
    return lastSyncDt;
    }
    
    public void setLastSyncDt(String lastSyncDt) {
    this.lastSyncDt = lastSyncDt;
    }
    
    public List<MeetingsInfo> getMeetingsInfo() {
    return meetingsInfo;
    }
    
    public void setMeetingsInfo(List<MeetingsInfo> meetingsInfo) {
    this.meetingsInfo = meetingsInfo;
    }
    
    }
    /*-----------------------------------com.example.MeetingsInfo.java-----------------------------------*/
    
    package com.example;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    public class MeetingsInfo {
    
    @SerializedName("date")
    @Expose
    private String date;
    @SerializedName("meeting_id")
    @Expose
    private String meetingId;
    @SerializedName("subject")
    @Expose
    private String subject;
    @SerializedName("customer_id")
    @Expose
    private String customerId;
    @SerializedName("customer_name")
    @Expose
    private String customerName;
    
    public String getDate() {
    return date;
    }
    
    public void setDate(String date) {
    this.date = date;
    }
    
    public String getMeetingId() {
    return meetingId;
    }
    
    public void setMeetingId(String meetingId) {
    this.meetingId = meetingId;
    }
    
    public String getSubject() {
    return subject;
    }
    
    public void setSubject(String subject) {
    this.subject = subject;
    }
    
    public String getCustomerId() {
    return customerId;
    }
    
    public void setCustomerId(String customerId) {
    this.customerId = customerId;
    }
    
    public String getCustomerName() {
    return customerName;
    }
    
    public void setCustomerName(String customerName) {
    this.customerName = customerName;
    }
    
    }
    

    现在你可以直接在模型类中设置你的数据,比如 Bellow

    Example example = new Gson().fromJson(jsonRespons, new TypeToken<Example>() {
                        }.getType());
    

    【讨论】:

    • 您将两个不同的模型类合并为一个。我需要被分开。 MeetingInfo 和 customerInfo
    • 可能是访问它的不正确方法,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 2011-02-12
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    相关资源
    最近更新 更多