使用GSON将JSON解析为Java POJO [英] Parsing JSON to Java POJO's using GSON
问题描述
到目前为止,它会将它们反序列化到我的List中,但每个Metric对象都有null
{
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,
na我:操作系统,
值:Linux,
sessionID:
}
},
{
公制:{
type:5,
name:browser,
value:Netscape,
sessionID:
code $
$ b $ p $
度量对象
public class Metric {
私有int类型;
私人字符串名称;
私有字符串值;
private String sessionID;
/ **
* @return类型
* /
public int getType(){
返回类型;
$ b $ **
* @param键入要设置的类型
* /
public void setType(int type){
this.type = type;
$ b / **
* @返回名称
* /
public String getName(){
return name;
}
/ **
* @param将名称设置为
* /
public void setName(String name){
this.name = name;
$ b / **
* @返回值
* /
public String getValue(){
return value;
}
/ **
* @param value设置的值
* /
public void setValue(String value){
this.value = value;
$ b / **
* @return sessionID
* /
public String getSessionID(){
return sessionID;
}
$ b $ **
* @param sessionID sessionID设置
* /
public void setSessionID(String sessionID){
this.sessionID = sessionID;
}
}
Container Ojbect
import java.util.List;
$ b $ **
* @author joshua
* /
public class MetricSet {
private List< Metric> ;指标;
/ **
* @返回指标
* /
public List< Metric> getMetrics(){
返回指标;
$ b $ **
* @param指标设置的指标
* /
public void setMetrics(List< Metric> metrics){
this.metrics = metrics;
代码转换JSON
字符串json =;
if(request.getParameter(data)!= null){
json = request.getParameter(data);
}
MetricSet metrics = new MetricSet();
尝试{
Gson gson = new Gson();
Type listType = new TypeToken< MetricSet>(){} .getType();
metrics = gson.fromJson(json,MetricSet.class);
catch(Exception ex){
String msg = ex.toString();
解决方案您可以创建相应的java类为JSON对象。
整数
,字符串
值可按原样映射。 JSON可以这样解析:Gson gson = new GsonBuilder()。create();
响应r = gson.fromJson(jsonString,Response.class);
以下是一个示例: http://rowsandcolumns.blogspot.com/2013/02/url-encode-http-get-solr- request-and.html
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(); }
解决方案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
这篇关于使用GSON将JSON解析为Java POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!