deserialize json array to java object using Jackson

谁说我不能喝 提交于 2020-01-15 11:54:10

问题


given the following 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"
            }
            ]
        }
    }
  }
}

I'm using the following code to call jackson:

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

I have ganerated POJOs for the whole hiearchy where the products class looks like:

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

}

and the hits class:

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
}

}

All the other properties are mapped correct, but not the list containing hits. It's all empty. How can I map this to get it right?

Thanks!


回答1:


You model is not compatible.

In order to see what is going wrong, maybe it is a good idea to have some toStrings and you could easy see where the mapping is failing.

You have a Object that needs to hold a property response, that needs to hold a property resultSets that needs to hold a property products that needs to hold hits.

I implemented like this:

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

Please test following implementation:

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 + "]";
    }

}

after all you can do something like:

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



回答2:


The code looks fine. The error may be in json response

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

The backquotes after 47 and before TX, might be troublesome. Please check if you can parse this response.



来源:https://stackoverflow.com/questions/10975685/deserialize-json-array-to-java-object-using-jackson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!