简体   繁体   English

如何在 Java 中将此 JSON 解析为我的 POJO

[英]How to parse this JSON to my POJO in Java

I have this JSON structure that I'm trying to parse to a Java Object but don't know what I'm missing here.我有这个 JSON 结构,我试图解析为 Java 对象,但不知道我在这里缺少什么。

{
   "response":{
      "status":1,
      "httpStatus":200,
      "data":{
         "94":{
            "Offer":{
               "id":"94",
               "name":"Survey Junkie Panel - US, CA, AU (SOI)",
               "description":"<b>Description:</b> Start earning cash and rewards by doing surveys.<br><br>\r\n<b>Requirement:</b> Email Submit<br><br>\r\n<b>Country(ies):</b> US, CA, AU<br><br>\r\n<b>Media:</b> Display, Email<br><br>\r\n<b>Restrictions:</b> No incentives; no search; no pops; no brokering; no users under 18; no SnapChat<br><br>\r\n<b>Other:</b> New pubs need to pause at 300 leads and ask to be evaluated (if higher test cap is necessary, contact your affiliate manager to discuss). If traffic quality is good, Advertiser will lift lead cap as long as quality remains consistent. New partners should not start on weekends. Contact publishers@pantherainteractive.com when you reach your assigned lead cap.<br><br>",
               "require_approval":"0",
               "require_terms_and_conditions":0,
               "terms_and_conditions":null,
               "preview_url":"https://www.surveyjunkie.com/",
               "currency":null,
               "default_payout":"1.20000",
               "protocol":"server",
               "status":"active",
               "expiration_date":"2030-12-23 04:59:59",
               "payout_type":"cpa_flat",
               "percent_payout":"0.00",
               "featured":null,
               "conversion_cap":"0",
               "monthly_conversion_cap":"0",
               "payout_cap":"0.00",
               "monthly_payout_cap":"0.00",
               "allow_multiple_conversions":"0",
               "allow_website_links":"0",
               "allow_direct_links":"0",
               "show_custom_variables":"0",
               "session_hours":"24",
               "show_mail_list":"0",
               "dne_list_id":"0",
               "email_instructions":"0",
               "email_instructions_from":"",
               "email_instructions_subject":"",
               "enforce_secure_tracking_link":"1",
               "has_goals_enabled":"0",
               "default_goal_name":"",
               "modified":1622672025,
               "use_target_rules":"0",
               "use_payout_groups":"0",
               "link_platform":"",
               "is_expired":"0",
               "dne_download_url":null,
               "dne_unsubscribe_url":null,
               "dne_third_party_list":false,
               "approval_status":"approved"
            },
            "Country":{
               "US":{
                  "id":"840",
                  "code":"US",
                  "name":"United States",
                  "regions":[
                     
                  ]
               },
               "AU":{
                  "id":"36",
                  "code":"AU",
                  "name":"Australia",
                  "regions":[
                     
                  ]
               },
               "CA":{
                  "id":"124",
                  "code":"CA",
                  "name":"Canada",
                  "regions":[
                     
                  ]
               }
            }
         }
      }
   }
}

I have offer data:我有报价数据:

@Getter @Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class OfferData {

  @JsonProperty("Offer")
  Offer offer;

  @JsonProperty("Country")
  Country country;

}

I'm already recovering de Offers correctly the problem is at the Country object.我已经在正确地恢复 de Offers 问题出在 Country 对象上。

The Country:国家:

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class Country {
  Map<String, CountryData> countryData;
}

The CountryData:国家数据:

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class CountryData {
  @JsonProperty("id")
  private String id;
  @JsonProperty("code")
  private String code;
  @JsonProperty("name")
  private String name;
  @JsonProperty("regions")
  List<Region> regions;
}

I'm getting this error:我收到此错误:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; Servlet.service() 用于路径 [] 上下文中的 servlet [dispatcherServlet] 引发异常 [请求处理失败; nested exception is org.springframework.web.client.RestClientException: Error while extracting response for type [class com.sample.surveydemo.model.dto.OfferMapper] and content type [application/json];嵌套异常是 org.springframework.web.client.RestClientException: Error while extracting response for type [class com.sample.surveydemo.model.dto.OfferMapper] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type com.sample.surveydemo.model.dto.Country from Array value (token JsonToken.START_ARRAY );嵌套异常是 org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法从数组值反序列化com.sample.surveydemo.model.dto.Country类型的值(令牌JsonToken.START_ARRAY ); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type com.sample.surveydemo.model.dto.Country from Array value (token JsonToken.START_ARRAY ) at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream);嵌套异常是JsonToken.START_ARRAY :无法反序列化类型com.sample.surveydemo.model.dto.Country的值来自 [Source: (org.springframework.util .StreamUtils$NonClosingInputStream); line: 1, column: 92576] (through reference chain: com.sample.surveydemo.model.dto.OfferMapper["response"]->com.sample.surveydemo.model.dto.OfferResponse["data"]->java.util.LinkedHashMap["266"]->com.sample.surveydemo.model.dto.OfferData["Country"])] with root cause行:1,列:92576](通过引用链:com.sample.surveydemo.model.dto.OfferMapper["response"]->com.sample.surveydemo.model.dto.OfferResponse["data"]->java .util.LinkedHashMap["266"]->com.sample.surveydemo.model.dto.OfferData["Country"])] 的根本原因

Go to https://www.jsonschema2pojo.org/ .转到https://www.jsonschema2pojo.org/ Copy and paste the JSON data and adjust the parameters (generate getters and setters, toString etc).复制并粘贴 JSON 数据并调整参数(生成 getter 和 setter、toString 等)。 Click on preview to see the converted JSON data in POJO format or you can download the zip file containing the POJO class.单击预览以查看转换后的 POJO 格式的 JSON 数据,或者您可以下载包含 POJO 类的 zip 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM