1

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!

2 Answers 2

5

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);
3
  • I'm sorry I didn't wrote all of my code, but my POJOs looks exactly the same as yours. But still not works...
    – jorgen
    Jun 11, 2012 at 7:55
  • if you prefer I can send you my implementation in which I tested it... another thing you could check is regarding the server encoding, maybe you are receiving something that is expected to be interpreted in other encoding... Jun 11, 2012 at 7:59
  • Thanks, but I solved it. It was just a stupid reference to a wrong bean. Thanks for all help!
    – jorgen
    Jun 11, 2012 at 8:03
0

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.

1
  • This is a response from a search service. Whatever I search for, doesn't matter if the title containing the backquotes or just normal text, the hits list is still empty.
    – jorgen
    Jun 11, 2012 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.