反序列化使用JSON杰克逊数组java对象(deserialize json array to ja

2019-10-16 20:43发布

给出以下JSON:

{ "response": {
"totalProcessingTime": "271.0",
"resultSets": {
    "products": {
        "firstHit": "1",
        "lastHit": "10",
        "totalHits": "77",
        "hits": [ 
            {   
                "number": "1",
                "dmsubcategory": "TV, lyd og bilde",
                "collection": "tilbud",
                "title": "<b>TV</b> Panasonic 47 TX-LU 7ET5Y"
            },
            {   
                "number": "2",
                "dmsubcategory": "TV, lyd og bilde",
                "collection": "tilbud",
                "title": "<b>TV</b> Panasonic 47 TX-LU 7ET5Y"
            },
            {   
                "number": "3",
                "dmsubcategory": "TV, lyd og bilde",
                "collection": "tilbud",
                "title": "<b>TV</b> Panasonic 47 TX-LU 7ET5Y"
            }
            ]
        }
    }
  }
}

我使用下面的代码来调用杰克逊:

ObjectMapper mapper = new ObjectMapper();
SearchResult searchResult = mapper.readValue(new URL(jsonUrl + queryUrl), SearchResult.class);

我已经ganerated的POJO整个hiearchy其中产品类的样子:

public class Products {

public List<Hits> hits;
public String totalHits;

@JsonAnySetter
public void handleUnknown(String key, Object value) {
    // do something: put to a Map; log a warning, whatever
}

public List<Hits> getHits() {
    return hits;
}

public void setHits(List<Hits> hits) {
    this.hits = hits;
}

public String getTotalHits() {
    return totalHits;
}

public void setTotalHits(String totalHits) {
    this.totalHits = totalHits;
}

}

和命中等级:

public class Hits {

public String number;
public String title;

public String getNumber() {
    return number;
}
public void setNumber(String number) {
    this.number = number;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}

@JsonAnySetter
public void handleUnknown(String key, Object value) {
    // do something: put to a Map; log a warning, whatever
}

}

所有其他属性都映射正确,但不包含命中列表。 这一切都为空。 我该如何映射此得到它吗?

谢谢!

Answer 1:

您模式不兼容。

为了看到了什么错误,也许是有一些toStrings一个好主意,你可以很容易看到其中映射失败。

您有需要持有物业的响应,需要保存需要保存需要持有命中属性产品属性的ResultSet的目的。

我实现这样的:

GeneralResponse
  - Response
    - ResultSets
      - Products
        - Hits
          - number
          - title

请测试以下实现:

package snippet;

public class GeneralResponse {

    private Response response;

    public Response getResponse() {
        return response;
    }

    public void setResponse(Response response) {
        this.response = response;
    }

    @Override
    public String toString() {
        return "GeneralResponse [response=" + response + "]";
    }

}


package snippet;

public class ResultSets {

    private Products products;

    public Products getProducts() {
        return products;
    }

    public void setProducts(Products products) {
        this.products = products;
    }

    @Override
    public String toString() {
        return "ResultSets [products=" + products + "]";
    }

}

package snippet;

import java.util.List;

import org.codehaus.jackson.annotate.JsonAnySetter;

public class Products {

    public List<Hits> hits;
    public String totalHits;

    @JsonAnySetter
    public void handleUnknown(String key, Object value) {
        // do something: put to a Map; log a warning, whatever
    }

    public List<Hits> getHits() {
        return hits;
    }

    public void setHits(List<Hits> hits) {
        this.hits = hits;
    }

    public String getTotalHits() {
        return totalHits;
    }

    public void setTotalHits(String totalHits) {
        this.totalHits = totalHits;
    }

    @Override
    public String toString() {
        return "Products [hits=" + hits + ", totalHits=" + totalHits + "]";
    }

}


package snippet;

import org.codehaus.jackson.annotate.JsonAnySetter;

public class Response {

    private ResultSets resultSets;

    public ResultSets getResultSets() {
        return resultSets;
    }

    public void setResultSets(ResultSets resultSets) {
        this.resultSets = resultSets;
    }

    @Override
    public String toString() {
        return "Response [resultSets=" + resultSets + "]";
    }

    @JsonAnySetter
    public void handleUnknown(String key, Object value) {
        // do something: put to a Map; log a warning, whatever
    }

}



package snippet;

import org.codehaus.jackson.annotate.JsonAnySetter;

public class Hits {

    public String number;
    public String title;

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @JsonAnySetter
    public void handleUnknown(String key, Object value) {
        // do something: put to a Map; log a warning, whatever
    }

    @Override
    public String toString() {
        return "Hits [number=" + number + ", title=" + title + "]";
    }

}

毕竟,你可以这样做:

    ObjectMapper om = new ObjectMapper();
    Object r = om.readValue(inputStream, GeneralResponse.class);


Answer 2:

该代码看起来不错。 该错误可能是在JSON响应

"title": "<b>TV</b> Panasonic 47”TX-LU 7ET5Y"

47后和TX之前的反引号,可能是麻烦。 请检查您是否可以解析此响应。



文章来源: deserialize json array to java object using Jackson