このJsonコードを解析したい:
[{"id":7,"key":"integrationContinue:integrationContinue","name":"life Portlet","scope":"PRJ","qualifier":"TRK","date":"2012-03-26T10:10:22+0100","lname":"life Portlet","lang":"java","version":"1.0-SNAPSHOT","description":"","msr":[{"key":"ncloc","val":897.0,"frmt_val":"897"},{"key":"coverage","val":0.6,"frmt_val":"0,6%"}]}]
私は2つのクラスを作成しました:
public class Ressources {
private String id;
private String key;
private String name;
private String lname;
private String scope;
private String qualifier;
private String lang;
private String version;
private String date;
private List<Mesures> msr;
public Ressources() {
}
public Ressources(String id, String key, String name, String lname,
String scope, String qualifier, String lang, String version,
String date, List<Mesures> msr) {
super();
this.id = id;
this.key = key;
this.name = name;
this.lname = lname;
this.scope = scope;
this.qualifier = qualifier;
this.lang = lang;
this.version = version;
this.date = date;
this.msr = msr;
}
@Override
public String toString() {return "Ressources : \n id=" + id + ",\n key=" + key + ",\n name=" + name + ",\n lname=" + lname + ",\n scope=" + scope + ",\n qualifier=" + qualifier + ",\n lang=" + lang + ",\n version=" + version + ",\n date=" + date;
}
public class Ressources {
private String id;
private String key;
private String name;
private String lname;
private String scope;
private String qualifier;
private String lang;
private String version;
private String date;
private List<Mesures> msr;
public Ressources() {
}
public Ressources(String id, String key, String name, String lname,
String scope, String qualifier, String lang, String version,
String date, List<Mesures> msr) {
super();
this.id = id;
this.key = key;
this.name = name;
this.lname = lname;
this.scope = scope;
this.qualifier = qualifier;
this.lang = lang;
this.version = version;
this.date = date;
this.msr = msr;
}
@Override
public String toString() {return "Ressources : \n id=" + id + ",\n key=" + key + ",\n name=" + name + ",\n lname=" + lname + ",\n scope=" + scope + ",\n qualifier=" + qualifier + ",\n lang=" + lang + ",\n version=" + version + ",\n date=" + date;}
(ゲッターとセッター付き)
public class Mesures {
private String key;
private float val;
private String frmt_val;
public Mesures(){}
public Mesures(String akey, float aval,String afrmt_val ){
key=akey;
val=aval;
frmt_val=afrmt_val;
}
@Override
public String toString() {return " \n key=" + key + ",\n val=" + val + ",\n frmt_val=" + frmt_val;
}
次に、2つの関数を作成しました。
public List<Ressources> parseGson_Ressources(String jsonToParse) {
JsonElement jsonElement = new JsonParser().parse(jsonToParse);
JsonArray array = jsonElement.getAsJsonArray();
@SuppressWarnings("rawtypes")
Iterator iterator = array.iterator();
List<Ressources> ressources = new ArrayList<Ressources>();
while (iterator.hasNext()) {
JsonElement jsontmp = (JsonElement) iterator.next();
Gson gson = new Gson();
Ressources ressource1 = gson.fromJson(jsontmp, Ressources.class);
ressources.add(ressource1);
}
return ressources;
}
public List<Mesures> parseGson_Mesures(String jsonToParse) {
JsonElement jsonElement = new JsonParser().parse(jsonToParse);
JsonArray array = jsonElement.getAsJsonArray();
@SuppressWarnings("rawtypes")
Iterator iterator = array.iterator();
List<Mesures> mesures = new ArrayList<Mesures>();
while (iterator.hasNext()) {
JsonElement jsontmp = (JsonElement) iterator.next();
Gson gson = new Gson();
Mesures mesure = gson.fromJson(jsontmp, Mesures.class);
mesures.add(mesure);
}
return mesures;
}
それから私は結果を得るためにこのコードを書きました:
String xxx = RequestSonar(); //wich return the Json result
List<Ressources> listRessources = new ArrayList<Ressources>();
listRessources = rs.parseGson_Ressources(xxx);
Iterator<Ressources> iterator;
iterator = listRessources.iterator();
while (iterator.hasNext()) {
Ressources ressource = iterator.next();
System.out.println(ressource.toString());
}
List<Mesures> listMesures = new ArrayList<Mesures>();
Iterator<Mesures> iterator2;
listMesures = rs.parseGson_Mesures(xxx);
iterator2 = listMesures.iterator();
while (iterator2.hasNext()) {
Mesures mesure = iterator2.next();
System.out.println(mesure.toString());
}
実行後、次の結果が得られます。
Ressources :
id=7,
key=integrationContinue:integrationContinue,
name=life Portlet,
lname=life Portlet,
scope=PRJ,
qualifier=TRK,
lang=java,
version=1.0-SNAPSHOT,
date=2012-03-26T10:10:22+0100
key=integrationContinue:integrationContinue,
val=0.0,
frmt_val=null
どうすればこれを修正できますか?