1

我想解析这个 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%"}]}]

我创建了两个类:

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

(带有 getter 和 setter)

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

然后我创建了两个函数:

 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

我怎样才能解决这个问题 ?

4

4 回答 4

4
i think this will help you,

create two classes with getter setter methods

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 String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getName() {
        return name;
    }

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

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public String getQualifier() {
        return qualifier;
    }

    public void setQualifier(String qualifier) {
        this.qualifier = qualifier;
    }

    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public List<Mesures> getMsr() {
        return msr;
    }

    public void setMsr(List<Mesures> msr) {
        this.msr = msr;
    }

}

public class Mesures {

    private String key;
    private float val;
    private String frmt_val;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public float getVal() {
        return val;
    }

    public void setVal(float val) {
        this.val = val;
    }

    public String getFrmt_val() {
        return frmt_val;
    }

    public void setFrmt_val(String frmt_val) {
        this.frmt_val = frmt_val;
    }

}

then use this to Parse your JSON

String jsonl = "[{'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%'}]}]";

Gson gson = new Gson();
Type collectionType = new TypeToken<List<Ressources>>() {
        }.getType();
List<Ressources> ressourcesList = gson.fromJson(jsonl, collectionType);

Ressources ressources = ressourcesList.get(0);
System.out.println("id :" + ressources.getId());
System.out.println("key :" + ressources.getKey());
System.out.println("name :" + ressources.getName());
System.out.println("scope :" + ressources.getScope());
System.out.println("qualifier :" + ressources.getQualifier());
System.out.println("date :" + ressources.getDate());
System.out.println("lname :" + ressources.getLname());
System.out.println("lang :" + ressources.getLang());
System.out.println("version :" + ressources.getVersion());
System.out.println("Mrs :");

List<Mesures> mrsList = ressources.getMsr();
for (int i = 0; i < mrsList.size(); i++) {
System.out.println("key :" + mrsList.get(i).getKey());
System.out.println("val :" + mrsList.get(i).getVal());
System.out.println("frmt_val :" + mrsList.get(i).getFrmt_val());
}


output:

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
Mrs :
key :ncloc
val :897.0
frmt_val :897
key :coverage
val :0.6
frmt_val :0,6%
于 2012-04-04T13:25:00.833 回答
3

你可以写:

public Ressources[] parseGson_Ressources(String jsonToParse) {
    Gson gson = new Gson();
    Ressources[] resources = gson.fromJson(jsonToParse, Ressources[].class);
}

这应该完成您的两个复杂方法parseGson_RessourcesparseGson_Ressources.

编辑根据 Guillaume Polet 的评论,我的初始代码不正确。我将方法的返回类型更改为数组,因为这更易于实现。不过,如果您想坚持List解决方案,请考虑他的评论 - 它应该会有所帮助。

于 2012-04-04T12:03:49.043 回答
0

I don't want to burst your bubble... but I would simply go:

new Gson().fromJson(<json string here>, MyClass.class)

MyClass would be the class that you generated the JSon from. The GSon is a Google library. If you use maven, you can include it as follows:

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>1.7.1</version>
</dependency>

It's so easy... to convert to JSon, simple use the toJson() method.

于 2012-04-04T12:08:05.840 回答
0

如果这个 JSON 是从 Sonar Web 服务调用的结果,那么您不需要解析它:只需使用 Sonar Java Web Service Client 组件,这将为您完成所有工作,您无需关心JSON => http://docs.codehaus.org/display/SONAR/Using+the+Web+Service+Java+client

于 2012-04-04T13:29:32.407 回答