Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON to Java POJO's using GSON

I have a very straight forward issue here. I need to take JSON coming from the API and convert it to objects I created for them.

This far, it will deserialize them into my List but each Metric object has null values

JSON COMING IN

{
"metrics": [
    {
        "metric": {
            "type": 1,
            "name": "slide-11-start",
            "value": "1287249598295",
            "sessionID": "" 
        } 
    },
    {
        "metric": {
            "type": 1,
            "name": "slide-21-start",
            "value": "1287249601368",
            "sessionID": "" 
        } 
    },
    {
        "metric": {
            "type": 7,
            "name": "resolution",
            "value": "1680x1050",
            "sessionID": "" 
        } 
    },
    {
        "metric": {
            "type": 6,
            "name": "OS",
            "value": "Linux",
            "sessionID": "" 
        } 
    },
    {
        "metric": {
            "type": 5,
            "name": "browser",
            "value": "Netscape",
            "sessionID": "" 
        } 
    } 
]

}

Metric Object

public class Metric {

    private int type;
    private String name;
    private String value;
    private String sessionID;

    /**
     * @return the type
     */
    public int getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(int type) {
        this.type = type;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * @param value the value to set
     */
    public void setValue(String value) {
        this.value = value;
    }

    /**
     * @return the sessionID
     */
    public String getSessionID() {
        return sessionID;
    }

    /**
     * @param sessionID the sessionID to set
     */
    public void setSessionID(String sessionID) {
        this.sessionID = sessionID;
    }

}

Container Ojbect

import java.util.List;

/**
 *
 * @author joshua
 */
public class MetricSet {

    private List<Metric> metrics;

    /**
     * @return the metrics
     */
    public List<Metric> getMetrics() {
        return metrics;
    }

    /**
     * @param metrics the metrics to set
     */
    public void setMetrics(List<Metric> metrics) {
        this.metrics = metrics;
    }
}

CODE TO CONVERT THE JSON

    String json = "";
    if(request.getParameter("data") != null) {
        json = request.getParameter("data");
    }

    MetricSet metrics = new MetricSet();

    try {
        Gson gson = new Gson();
        Type listType = new TypeToken<MetricSet>() {}.getType();
        metrics = gson.fromJson(json, MetricSet.class);
    }
    catch(Exception ex) {
        String msg = ex.toString();
    }
like image 278
Ethode Avatar asked Oct 16 '10 17:10

Ethode


1 Answers

You can create corresponding java classes for the JSON objects. The integer, string values can be mapped as is. JSON can be parsed like this:

 Gson gson = new GsonBuilder().create();
 Response r = gson.fromJson(jsonString, Response.class);

Here is an example: http://rowsandcolumns.blogspot.com/2013/02/url-encode-http-get-solr-request-and.html

like image 183
blue01 Avatar answered Oct 27 '22 00:10

blue01