解析Sonar的Json输出

时间:2016-06-02 09:47:56

标签: java json web-services

我正在调用一个声纳web服务并以json格式获得低于输出。

[  
   {  
      "id":10252,
      "uuid":"ca49aeed-de29-41a1-b0e2-e2b7c7d1b6c5",
      "key":"UTILITY",
      "name":"UTILITY",
      "scope":"PRJ",
      "qualifier":"VW",
      "date":"2012-05-02T05:07:04-0400",
      "creationDate":"2009-03-12T09:03:35-0400",
      "lname":"UTILITY",
      "msr":[  
         {  
            "key":"ncloc",
            "val":253603.0,
            "frmt_val":"253,603"
         },
         {  
            "key":"test_success_density",
            "val":85.5,
            "frmt_val":"85.5%"
         },
         {  
            "key":"coverage",
            "val":96.0,
            "frmt_val":"96.0%"
         }
      ]
   }
]

现在我想在java中解析这个输出并获取date,ncloc,test_success_density和coverage的值。我该怎么做?我尝试了很多java apis,但在获取上述字段的值时遇到了麻烦。

1 个答案:

答案 0 :(得分:0)

同样,

注意:包括合适的Jackson Jar(我使用的是jackson-core-2.2.3.jar)

您的主要课程应该是......

import java.util.Date;
import java.util.List;

public class Bean {

    private long id;
    private String uuid;
    private String key;
    private String name; 
    private String scope;
    private String qualifier;
    private Date date;
    private Date creationDate;
    private String lname;
    private List<Msr> msr;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getUuid() {
        return uuid;
    }
    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
    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 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 Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public Date getCreationDate() {
        return creationDate;
    }
    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }
    public String getLname() {
        return lname;
    }
    public void setLname(String lname) {
        this.lname = lname;
    }
    public List<Msr> getMsr() {
        return msr;
    }
    public void setMsr(List<Msr> msr) {
        this.msr = msr;
    }

    @Override
    public String toString() {
        return this.id + " : " +
                this.uuid + " : " +
                this.key + " : " +
                this.name + " : " +
                this.scope + " : " +
                this.qualifier + " : " +
                this.date + " : " +
                this.creationDate + " : " +
                this.lname + " : " +
                this.msr ;
    }

}

class Msr{

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

    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public String getVal() {
        return val;
    }
    public void setVal(String val) {
        this.val = val;
    }
    public String getFrmt_val() {
        return frmt_val;
    }
    public void setFrmt_val(String frmt_val) {
        this.frmt_val = frmt_val;
    } 

    @Override
    public String toString() {
        return this.key + " : " + this.val + " : " + this.frmt_val;
    }

}

让我们说你的Bean.java会是,

<div id="bar">
    <table class = "BarTable" style="width:100%;height: 100%">
      <tr>
        <td style="width: 35%">
           <img src="131AZE.jpg" style="height:100%;width: 100%">
        </td>
        <td>Everyone</td>
       </tr>
    </table>
</div>

做上述事情,完美运作...... !!

相关问题