【问题标题】:mapper.readValue() return null value (Jackson API)mapper.readValue() 返回空值(杰克逊 API)
【发布时间】:2017-02-14 08:26:56
【问题描述】:

我有一个 JSON 对象,我正试图从 Jackson API 对象映射器中读取它。

{  
   "ddt_id":"605",
   "ddt_batch_code":"5769005b-e8f0-4ae8-8971-1c59ac1f02fd",
   "keyword":"ADP",
   "keyword_operation":"and",
   "keyword_extract_match":"F",
   "search_in":"name",
   "filter_type":"entity,others",
   "category":"2,3,5",
   "gender":"",
   "date_year":"",
   "date_month":"",
   "date_day":"",
   "country":"",
   "search_filter_uuid":"570bd722-315c-40b3-b2d6-4522ac1f02fd",
   "ddt_qsk_question":"0",
   "search_for":"all",
   "search_category":"2,3,5",
   "search_includes_name":"T",
   "search_includes_profile_notes":"F",
   "search_for_person":"F",
   "search_for_entity":"T",
   "search_for_others":"T",
   "search_from_module":"DDMT.V.2.20",
   "client_id":667,
   "ip_address":"52.23.94.13",
   "search_requester_id":false,
   "search_requester_name":false,
   "batch_id":"5769005b-e8f0-4ae8-8971-1c59ac1f02fd",
   "person_query_index":4,
   "company_query_index":4,
   "is_ongoing":1
}

我用来在 Object 中读取这个 JSON 的 Class 是:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.UUID;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class SswltSearchParams {
    @JsonProperty("batch_id")
    private UUID batchId;

    @JsonProperty("country")
    private String country;

    @JsonProperty("criteria_id")
    private String id;

    @JsonProperty("date_day")
    private Integer day;

    @JsonProperty("date_month")
    private Integer month;

    @JsonProperty("date_year")
    private Integer year;

    @JsonProperty("gender")
    private String gender;

    @JsonProperty("keyword")
    private String keyword;

    @JsonProperty("keyword_exact_match")
    private String keywordExactMatch;

    @JsonProperty("keyword_operation")
    private String keywordOperation;

    @JsonProperty("search_category")
    private String searchCategory;

    @JsonProperty("search_for")
    private String searchFor;

    @JsonProperty("search_for_anti_corruption")
    private String searchForAntiCorruption;

    @JsonProperty("search_for_entity")
    private String searchForEntity;

    @JsonProperty("search_for_others")
    private String searchForOthers;

    @JsonProperty("search_for_person")
    private String searchForPerson;

    @JsonProperty("search_for_watchlist")
    private String searchForWatchlist;

    @JsonProperty("search_includes_name")
    private String searchIncludesName;

    @JsonProperty("search_includes_profile_notes")
    private String searchIncludesProfileNotes;

    @JsonProperty("update_only")
    private String updateOnly;

   // getters and setters
}

当我尝试将此 JSON 放入 Onject 时,我没有收到任何错误,但我收到了 NULL 值。

try {
            SswltMigrationCollection sswltSearchParams = mapper.readValue(searchCriteria.getScrCriteria(), SswltSearchParams.class);
        } catch (IOException e) {
            return null;
        }

为什么我将此 sswltSearchParams 设为空?请帮忙。

【问题讨论】:

  • “我没有收到任何错误”,因为显示的单个 catch 块是您的最后一个 sn-p 忽略捕获的异常。你确定它不会抛出 IOException 吗?
  • 它也不会捕获块。没有错误,没有例外。

标签: java json jackson


【解决方案1】:

您的 JSON 可以毫无问题地解析为 SswltSearchParams 实例。以下代码工作正常:

String json = "{\"ddt_id\":\"605\",\"ddt_batch_code\":\"5769005b-e8f0-4ae8-8971-1c59ac1f02fd\",\"keyword\":\"ADP\",\"keyword_operation\":\"and\",\"keyword_extract_match\":\"F\",\"search_in\":\"name\",\"filter_type\":\"entity,others\",\"category\":\"2,3,5\",\"gender\":\"\",\"date_year\":\"\",\"date_month\":\"\",\"date_day\":\"\",\"country\":\"\",\"search_filter_uuid\":\"570bd722-315c-40b3-b2d6-4522ac1f02fd\",\"ddt_qsk_question\":\"0\",\"search_for\":\"all\",\"search_category\":\"2,3,5\",\"search_includes_name\":\"T\",\"search_includes_profile_notes\":\"F\",\"search_for_person\":\"F\",\"search_for_entity\":\"T\",\"search_for_others\":\"T\",\"search_from_module\":\"DDMT.V.2.20\",\"client_id\":667,\"ip_address\":\"52.23.94.13\",\"search_requester_id\":false,\"search_requester_name\":false,\"batch_id\":\"5769005b-e8f0-4ae8-8971-1c59ac1f02fd\",\"person_query_index\":4,\"company_query_index\":4,\"is_ongoing\":1}";

ObjectMapper mapper = new ObjectMapper();
SswltSearchParams sswltSearchParams = mapper.readValue(json, SswltSearchParams.class);

不知道为什么SswltMigrationCollection 类会发挥作用。

【讨论】:

    猜你喜欢
    • 2017-11-30
    • 2020-06-30
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    相关资源
    最近更新 更多