ev0lution37
ev0lution37

Reputation: 1179

Parsing JSON to Java Class with Gson

I'm having trouble parsing a JSON to a Java class using GSON. Here's the JSON:

{
    "results": {
        "searchCriteria": {
            "start": "2002-01-01",
            "end": "2015-07-06",
            "query": "test",
            "lat": "45.9511",
            "lon": "1.4063",
            "population": 5000,
            "popop": ">",
            "range": 50000
        },
        "citiesDetailed": [{
            "lon": 0.90143,
            "weight": 1,
            "count": 1,
            "name": "Saint-Junien",
            "lat": 45.88867
        },
        {
            "lon": 1.2578,
            "weight": 1,
            "count": 405,
            "name": "Limoges",
            "lat": 45.83153
        },
        {
            "lon": 1.21213,
            "weight": 10,
            "count": 25789,
            "name": "Isle",
            "lat": 45.80272
        },
        {
            "lon": 1.86667,
            "weight": 1,
            "count": 400,
            "name": "Gueret",
            "lat": 46.16667
        },
        {
            "lon": 1.40063,
            "weight": 1,
            "count": 2,
            "name": "Ambazac",
            "lat": 45.95983
        }]

    }
}

And here's my classes.

Results.java

package com.test.classes;

import java.util.List;

public class Results{
    private List citiesDetailed;
    private SearchCriteria searchCriteria;

    public List getCitiesDetailed(){
        return this.citiesDetailed;
    }
    public void setCitiesDetailed(List citiesDetailed){
        this.citiesDetailed = citiesDetailed;
    }
    public SearchCriteria getSearchCriteria(){
        return this.searchCriteria;
    }
    public void setSearchCriteria(SearchCriteria searchCriteria){
        this.searchCriteria = searchCriteria;
    }
}

SearchCriteria.java

package com.test.classes;

public class SearchCriteria{
    private String end;
    private String lat;
    private String lon;
    private String popop;
    private Number population;
    private String query;
    private Number range;
    private String start;

    public String getEnd(){
        return this.end;
    }
    public void setEnd(String end){
        this.end = end;
    }
    public String getLat(){
        return this.lat;
    }
    public void setLat(String lat){
        this.lat = lat;
    }
    public String getLon(){
        return this.lon;
    }
    public void setLon(String lon){
        this.lon = lon;
    }
    public String getPopop(){
        return this.popop;
    }
    public void setPopop(String popop){
        this.popop = popop;
    }
    public Number getPopulation(){
        return this.population;
    }
    public void setPopulation(Number population){
        this.population = population;
    }
    public String getQuery(){
        return this.query;
    }
    public void setQuery(String query){
        this.query = query;
    }
    public Number getRange(){
        return this.range;
    }
    public void setRange(Number range){
        this.range = range;
    }
    public String getStart(){
        return this.start;
    }
    public void setStart(String start){
        this.start = start;
    }
}

CitiesDetailed.java

package com.test.classes;

public class CitiesDetailed{
    private Number count;
    private Number lat;
    private Number lon;
    private String name;
    private Number weight;

    public Number getCount(){
        return this.count;
    }
    public void setCount(Number count){
        this.count = count;
    }
    public Number getLat(){
        return this.lat;
    }
    public void setLat(Number lat){
        this.lat = lat;
    }
    public Number getLon(){
        return this.lon;
    }
    public void setLon(Number lon){
        this.lon = lon;
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }
    public Number getWeight(){
        return this.weight;
    }
    public void setWeight(Number weight){
        this.weight = weight;
    }
}

And I'm simply using this for parsing it (String json is equal the JSON above):

Gson gson = new Gson();
Results r = gson.fromJson(json, Results.class);

System.out.println("Query: " + r.getSearchCriteria().getQuery());

This results in a NullPointerException, so clearly my mapping is off somewhere. I can't figure out where.

Upvotes: 0

Views: 98

Answers (1)

azurefrog
azurefrog

Reputation: 10955

The problem is that your json only has a single entry in it called results, so you are one layer short of being able to parse it properly.

You need to make a class to hold the Results object being returned and use Gson to parse your json into that.

For instance you can create ResultWrapper.java like so:

public class ResultWrapper {
    Results results;

    public Results getResults() {
        return results;
    }

    public void setResults(Results results) {
        this.results = results;
    }
}

...then make your parsing code look like this:

    Gson gson = new Gson();
    ResultWrapper rw = gson.fromJson(json, ResultWrapper.class);
    System.out.println("Query: " + rw.getResults().getSearchCriteria().getQuery());

...and you'll get output like so:

Query: test

Upvotes: 1

Related Questions