2010-10-16 23 views
5

Ho un problema molto semplice qui. Devo prendere JSON proveniente dall'API e convertirlo in oggetti che ho creato per loro.Analisi da JSON a Java POJO usando GSON

Questo lontano, li deserializzare nella mia lista ma ogni oggetto metrica ha valori nulli

JSON IN ARRIVO 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 oggetto

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

CODICE per convertire il 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(); 
    } 

risposta

2

(Sono passati solo 7,5 mesi.)

Il problema è che la struttura di classe che si sta cercando deserializzare in modo che non corrisponda alla struttura JSON.

Di seguito è riportato un semplice esempio che consente di deserializzare il JSON fornito. L'uscita è

[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=]]]]
public class Foo 
{ 
    static String jsonInput = 
    "{" + 
     "\"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\":\"\"" + 
      "}" + 
     "}" + 
     "]" + 
    "}"; 

    public static void main(String[] args) 
    { 
    GsonBuilder gsonBuilder = new GsonBuilder(); 
    Gson gson = gsonBuilder.create(); 
    Metrics metrics = gson.fromJson(jsonInput, Metrics.class); 
    System.out.println(metrics); 
    } 
} 

class Metrics 
{ 
    private List<MetricContainer> metrics; 

    @Override 
    public String toString() 
    { 
    return String.format(
     "[Metrics: %1$s]", 
     metrics); 
    } 
} 

class MetricContainer 
{ 
    private Metric metric; 

    @Override 
    public String toString() 
    { 
    return String.format(
     "[MetricContainer: %1$s]", 
     metric); 
    } 
} 

class Metric 
{ 
    private int type; 
    private String name; 
    private String value; 
    private String sessionID; 

    @Override 
    public String toString() 
    { 
    return String.format(
     "[Metric: type=%1$d, name=%2$s, value=%3$s, sessionID=%4$s]", 
     type, name, value, sessionID); 
    } 
} 
0

Si può dare un'occhiata a Gson Guide.

di analisi JSON a Java POJO di utilizzare GSON:

public class Response { 

private List<MetricsBean> metrics; 

public List<MetricsBean> getMetrics() { 
    return metrics; 
} 

public void setMetrics(List<MetricsBean> metrics) { 
    this.metrics = metrics; 
} 

public static class MetricsBean { 
    /** 
    * metric : {"type":1,"name":"slide-11-start","value":"1287249598295","sessionID":""} 
    */ 

    private MetricBean metric; 

    public MetricBean getMetric() { 
     return metric; 
    } 

    public void setMetric(MetricBean metric) { 
     this.metric = metric; 
    } 

    public static class MetricBean { 
     /** 
     * type : 1 
     * name : slide-11-start 
     * value : 1287249598295 
     * sessionID : 
     */ 

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

     public int getType() { 
      return type; 
     } 

     public void setType(int type) { 
      this.type = type; 
     } 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 

     public String getValue() { 
      return value; 
     } 

     public void setValue(String value) { 
      this.value = value; 
     } 

     public String getSessionID() { 
      return sessionID; 
     } 

     public void setSessionID(String sessionID) { 
      this.sessionID = sessionID; 
     } 
    } 
    } 
} 
Problemi correlati