将JSON解析为JavaPOJO';s正在使用GSON

将JSON解析为JavaPOJO';s正在使用GSON,json,parsing,serialization,gson,Json,Parsing,Serialization,Gson,我这里有一个非常直截了当的问题。我需要从API获取JSON并将其转换为我为它们创建的对象 到目前为止,它会将它们反序列化到我的列表中,但每个度量对象都有空值 JSON即将推出 { "metrics": [ { "metric": { "type": 1, "name": "slide-11-start", "value": "1287249598295", "sessionID

我这里有一个非常直截了当的问题。我需要从API获取JSON并将其转换为我为它们创建的对象

到目前为止,它会将它们反序列化到我的列表中,但每个度量对象都有空值

JSON即将推出

{
"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": "" 
        } 
    } 
]
    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();
    }
}

公制对象

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

}
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;
    }
}
集装箱Ojbect

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

}
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;
    }
}
import java.util.List;
/**
*
*@作者约书亚
*/
公共类度量集{
私有列表度量;
/**
*@返回度量值
*/
公共列表getMetrics(){
回报指标;
}
/**
*@param metrics要设置的度量
*/
公共void setMetrics(列出指标){
这个。度量=度量;
}
}
转换JSON的代码

{
"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": "" 
        } 
    } 
]
    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();
    }
String json=”“;
if(request.getParameter(“数据”)!=null){
json=request.getParameter(“数据”);
}
MetricSet metrics=新MetricSet();
试一试{
Gson Gson=新的Gson();
类型listType=newTypeToken(){}.getType();
metrics=gson.fromJson(json,MetricSet.class);
}
捕获(例外情况除外){
字符串msg=ex.toString();
}
(才7.5个月。)

问题是您试图反序列化到的类结构与JSON结构不匹配

下面是一个简单的示例,用于反序列化提供的JSON。输出是

[Metrics: [ [MetricContainer: [Metric: type=1, name=slide-11-start, value=1287249598295, sessionID=]], [MetricContainer: [Metric: type=1, name=slide-21-start, value=1287249601368, sessionID=]], [MetricContainer: [Metric: type=7, name=resolution, value=1680x1050, sessionID=]], [MetricContainer: [Metric: type=6, name=OS, value=Linux, sessionID=]], [MetricContainer: [Metric: type=5, name=browser, value=Netscape, sessionID=]]]] [指标:[ [MetricContainer:[Metric:type=1,name=slide-11-start,value=1287249598295,sessionID=], [MetricContainer:[Metric:type=1,name=slide-21-start,value=1287249601368,sessionID=], [MetricContainer:[Metric:type=7,name=resolution,value=1680x1050,sessionID=]], [MetricContainer:[Metric:type=6,name=OS,value=Linux,sessionID=], [MetricContainer:[Metric:type=5,name=browser,value=Netscape,sessionID=]]
公共类Foo
{
静态字符串jsonInput=
"{" + 
“\“指标\”:”+
"[" + 
"{" + 
“\“公制\”:”+
"{" + 
“\”类型\”:1,“+
“\”名称“:\”幻灯片-11-开始“,“+
“\”值\“:\”1287249598295\”,“+
“\”会话ID\”:\“\”+
"}" + 
"}," + 
"{" + 
“\“公制\”:”+
"{" + 
“\”类型\”:1,“+
“\”名称“:\”幻灯片-21-开始“,“+
““值”:“1287249601368\”,“+
“\”会话ID\”:\“\”+
"}" + 
"}," + 
"{" + 
“\“公制\”:”+
"{" + 
“\”类型\”:7,“+
“\”名称“:\”决议“,“+
““值”:“1680x1050\”,“+
“\”会话ID\”:\“\”+
"}" + 
"}," + 
"{" + 
“\“公制\”:”+
"{" + 
“\”类型\”:6,“+
“\”名称“:\”操作系统“,“+
“\'value\”:\'Linux\”,“+
“\”会话ID\”:\“\”+
"}" + 
"}," + 
"{" + 
“\“公制\”:”+
"{" + 
“\”类型\”:5,“+
“\”名称“:\”浏览器“,“+
“\”值\“:\”网景\“,“+
“\”会话ID\”:\“\”+
"}" + 
"}" +  
"]" + 
"}";
公共静态void main(字符串[]args)
{
GsonBuilder GsonBuilder=新的GsonBuilder();
Gson-Gson=gsonBuilder.create();
Metrics-Metrics=gson.fromJson(jsonInput,Metrics.class);
System.out.println(度量);
}
}
类度量
{
私有列表度量;
@凌驾
公共字符串toString()
{
返回字符串格式(
“[指标:%1$s]”,
指标);
}
}
类度量容器
{
专用公制;
@凌驾
公共字符串toString()
{
返回字符串格式(
“[MetricContainer:%1$s]”,
公制);
}
}
类度量
{  
私有int型;
私有字符串名称;
私有字符串值;
私有字符串sessionID;
@凌驾
公共字符串toString()
{
返回字符串格式(
“[度量:类型=%1$d,名称=%2$s,值=%3$s,会话ID=%4$s]”,
类型、名称、值、会话ID);
}
}

您可以为JSON对象创建相应的java类。
整数
字符串
值可以按原样映射。JSON可以这样解析:

 Gson gson = new GsonBuilder().create();
 Response r = gson.fromJson(jsonString, Response.class);
以下是一个例子: