无法使用gson库从SonarQube反序列化JSON响应

时间:2017-03-21 14:12:42

标签: java sonarqube gson

我正在尝试使用gson反序列化我从SonarQube API获取的各种代码指标上的一些数据。这是从服务器返回的原始JSON的示例:

{
  "component": {
    "id": "c5fc9d6k-e28b-4ea0-8922-df18c7e07ac1",
    "key": "APP:master",
    "name": "master",
    "qualifier": "TRK",
    "measures": [
      {
        "metric": "coverage",
        "value": "19.9",
        "periods": [
          {
            "index": 1,
            "value": "0.09999999999999787"
          },
          {
            "index": 2,
            "value": "0.09999999999999787"
          },
          {
            "index": 3,
            "value": "0.6999999999999993"
          },
          {
            "index": 4,
            "value": "8.7"
          }
        ]
      },
      {
        "metric": "overall_coverage",
        "value": "55.7",
        "periods": [
          {
            "index": 1,
            "value": "0.0"
          },
          {
            "index": 2,
            "value": "0.0"
          },
          {
            "index": 3,
            "value": "3.0"
          },
          {
            "index": 4,
            "value": "55.7"
          }
        ]
      },
      {
        "metric": "ncloc",
        "value": "1089127",
        "periods": [
          {
            "index": 1,
            "value": "3835"
          },
          {
            "index": 2,
            "value": "3835"
          },
          {
            "index": 3,
            "value": "-74350"
          },
          {
            "index": 4,
            "value": "102501"
          }
        ]
      }
    ]
  }
}

我正在尝试使用以下代码将其反序列化为Component类:

public Component getComponentMeasures(String componentKey, List<String> measures) throws ClientProtocolException,
        IOException, JsonSyntaxException, UnsupportedOperationException, JSONException 
{
    HttpGet request = new HttpGet(baseURL + String.format("/api/measures/component?componentKey=%s&metricKeys=%s",
            componentKey, StringUtils.join(measures, ",")));

    HttpResponse response = client.execute(request);

    Gson gson = new Gson();
    String componenta = getJSONResponse(response);
    System.out.print(componenta);
    Component component = gson.fromJson(componenta, Component.class);

    return component;
}

这是我将其反序列化为的组件类:

public class Component {

    @SerializedName("id")
    @Expose
    private String id;

    @SerializedName("key")
    @Expose
    private String key;

    @SerializedName("name")
    @Expose
    private String name;

    @SerializedName("qualifier")
    @Expose
    private String qualifier;

    @SerializedName("path")
    @Expose
    private String path;

    @SerializedName("measures")
    @Expose
    private Measure[] measures = null;

    public String getId() {
        return id;
    }

    public String getKey() {
        return key;
    }

    public String getName() {
        return name;
    }

    public String getQualifier() {
        return qualifier;
    }

    public String getPath() {
        return path;
    }

    public Measure[] getMeasures() {
        return measures;
    }

}

此Component类还包含一个Measures数组,而这些Measures又包含一个句点数组。

测量类:

public class Measure {

    @SerializedName("metric")
    @Expose
    private String metric;

    @SerializedName("value")
    @Expose
    private String value;

    @SerializedName("periods")
    @Expose
    private Period[] periods = null;

    public String getMetric() {
        return metric;
    }

    public String getValue() {
        return value;
    }

    public Period[] getPeriods() {
        return periods;
    }

}

期间课程:     公共课时段{

    @SerializedName("index")
    @Expose
    private Integer index;
    @SerializedName("value")
    @Expose
    private String value;

    public Integer getIndex() {
    return index;
    }

    public String getValue() {
    return value;
    }

}

当我运行此代码时,反序列化的组件为null。关于我可能在这里做错的任何想法?请注意,Component类中有一个额外的参数“path”,在JSON中为null。这是可选的,存在于包含Component对象集合的其他类中。在这些情况下,此Component对象和JSON反序列化很好。我将JSON并排比较,它们完全相同。在尝试反序列化独立组件对象时,我似乎只有这个问题。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

请注意,您的JSON文档是一个JSON对象,其中包含一个带有嵌套组件的属性(路径:$.component),但是您尝试将其反序列化,就好像它是最顶层的对象:

Component component = gson.fromJson(componenta, Component.class);

只需创建另一个类来匹配最顶层的单个属性对象,例如:

final class Response {

    @SerializedName("component")
    @Expose
    final Component component = null;

}

然后示例代码如

final Response response = gson.fromJson(componenta, Response.class);
for ( final Measure measure : response.component.measures ) {
    System.out.println(measure.metric + " " + measure.value);
}

将打印以下输出:

  

报道19.9
  overall_coverage 55.7
  ncloc 1089127

答案 1 :(得分:-1)

Sonar有一个用于API的SDK,它封装了所有这些,你不必创建自己的类等。我用它来获取信息,就像你正在做的那样。请参阅https://docs.sonarqube.org/display/SONARQUBE45/Using+the+Web+Service+Java+client以及我对它的使用:http://myvogonpoetry.com/wp/2013/02/21/using-the-sonar-rest-api-for-weekly-emails/