将Json解析为Java

将Json解析为Java,java,json,list,parsing,sonarqube,Java,Json,List,Parsing,Sonarqube,我想解析这个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

我想解析这个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 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;
    }

我如何解决这个问题?

您可以写:

public Ressources[] parseGson_Ressources(String jsonToParse) {
    Gson gson = new Gson();
    Ressources[] resources = gson.fromJson(jsonToParse, Ressources[].class);
}
这将完成parseGson_资源和parseGson_资源这两个复杂方法的所有工作


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

< P>你可以写下:

public Ressources[] parseGson_Ressources(String jsonToParse) {
    Gson gson = new Gson();
    Ressources[] resources = gson.fromJson(jsonToParse, Ressources[].class);
}
这将完成parseGson_资源和parseGson_资源这两个复杂方法的所有工作


根据Guillaume Polet的评论编辑我的初始代码不正确。我将方法的返回类型更改为array,因为这更易于实现。不过,如果你想坚持列表的解决方案,考虑他的评论-它应该是有帮助的。< / P> < P>我不想破坏你的泡沫。但我只想说:

new Gson().fromJson(<json string here>, MyClass.class)
MyClass将是从中生成JSon的类。GSon是一个谷歌图书馆。如果您使用maven,可以按如下方式包括它:

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

这太容易了。。。要转换为JSon,只需使用toJson方法。

我不想让你的泡沫破灭。。。但我只想说:

new Gson().fromJson(<json string here>, MyClass.class)
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%
MyClass将是从中生成JSon的类。GSon是一个谷歌图书馆。如果您使用maven,可以按如下方式包括它:

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


这太容易了。。。要转换为JSon,只需使用toJson方法。

如果此JSon是Sonar Web服务调用的结果,则无需解析它:只需使用Sonar Java Web服务客户端组件,这将为您完成所有这些,您根本不需要关心JSON=>

如果此JSON是来自Sonar Web服务的调用的结果,那么您不需要解析它:只需使用Sonar Java Web服务客户端组件,这将为您完成所有这些,您根本不需要关心JSON=>

您是否编写了自己的代码来解析java中的JSON?哇,这很值得一看。尝试使用GSON,您所需要的只是您的two-ValueObject类,应该足够好了。除非轮子坏了,否则不要重新发明轮子。我想他已经在使用GSon了。我觉得有一个库可以让你的工作更轻松,bug更少。检查这个问题:他已经在使用Gson了!唯一的问题是他没有正确地反序列化列表/集合,他使用了一种被窃听的混合技术。正确地将GSon与列表/集合结合使用将解决他的问题。您是否编写了自己的代码来用java解析JSON?哇,这很值得一看。尝试使用GSON,您所需要的只是您的two-ValueObject类,应该足够好了。除非轮子坏了,否则不要重新发明轮子。我想他已经在使用GSon了。我觉得有一个库可以让你的工作更轻松,bug更少。检查这个问题:他已经在使用Gson了!唯一的问题是他没有正确地反序列化列表/集合,他使用了一种被窃听的混合技术。在列表/集合中正确使用GSon将解决他的问题。顺便说一句,GSon现在是2.1版。他唯一的问题是,他没有使用列表/集合反序列化/序列化,而这正是他所做的。顺便说一句,GSon现在是2.1版。他唯一的问题是,他没有使用List/Collection反序列化/serialization纠正我的错误,但是List.class在Java中实际上是无效的。但是他可以在GSon中使用这个选项:Type collectionType=newtypetoken{}.getType;并传递collectionType而不是类。如果我错了,请纠正我,但List.class在Java中实际上无效。但是他可以在GSon中使用这个选项:Type collectionType=newtypetoken{}.getType;并传递collectionType而不是类。我尝试了它,但当我执行时,我得到了这个错误java.lang.NoClassDefFoundError:org/json/simple/JSONValueI尝试了这个资源struts=sonar.findResourceQuery.createFormetricIntegrationContinue:integrationContinue,coverage,lines,violations;度量s=struts.getmeasurecorerage;System.out.printlns.getData;您是否在使用Maven构建项目?如果没有,那么您应该:它将为您设置使用JavaWeb服务客户端组件所需的所有依赖项。我坚持认为:我们开发了这个Java客户机,因此您不需要重新创建所有JSON解析内容。一旦你的类路径正确了,你上面写的代码就可以做你需要的任何事情,就这么简单!我使用的是Maven,我添加了这个依赖项:org.codehaus.sonar你能帮我知道我应该放哪些依赖项吗。我正在使用maven、vaadin、Liferay删除所有其他JSON依赖项,您不需要它们。如果您按照文档进行操作,您会发现在页面的开头不需要太多依赖项
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%

eI尝试了以下资源struts=sonar.findResourceQuery.CreateFormetricIntegrationContinue:integrationContinue、coverage、lines、violations;度量s=struts.getmeasurecorerage;System.out.printlns.getData;您是否在使用Maven构建项目?如果没有,那么您应该:它将为您设置使用JavaWeb服务客户端组件所需的所有依赖项。我坚持认为:我们开发了这个Java客户机,因此您不需要重新创建所有JSON解析内容。一旦你的类路径正确了,你上面写的代码就可以做你需要的任何事情,就这么简单!我使用的是Maven,我添加了这个依赖项:org.codehaus.sonar你能帮我知道我应该放哪些依赖项吗。我正在使用maven、vaadin、Liferay删除所有其他JSON依赖项,您不需要它们。如果按照文档进行操作,您将看到在页面的开头不需要很多依赖项。