使用Jackson将json数组反序列化为java对象

时间:2012-06-11 07:01:56

标签: java json jackson pojo

给出以下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"
            }
            ]
        }
    }
  }
}

我正在使用以下代码来调用jackson:

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

我为产品类看起来的整个层次结构制作了POJO:

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
}

}

所有其他属性都映射正确,但不包含匹配的列表。这一切都是空的。我如何映射这个以使其正确?

谢谢!

2 个答案:

答案 0 :(得分:5)

你的模特不兼容。

为了看出出了什么问题,也许最好有一些toStrings,你可以很容易地看到映射失败的地方。

你有一个需要持有属性响应的对象,需要持有一个属性resultSets,它需要持有需要保持命中的属性产品。

我这样实施:

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);

答案 1 :(得分:0)

代码看起来很好。错误可能在json响应中

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

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