java - 无法使用 gson 库反序列化来自 SonarQube 的 JSON 响应

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

这是我将其反序列化到的 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;
    }

}

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

最佳答案

请注意,您的 JSON 文档是一个具有单个属性(路径:$.component)和一个嵌套组件的 JSON 对象,但是您正在尝试反序列化就好像它是最顶层的对象:

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

将打印以下输出:

coverage 19.9
overall_coverage 55.7
ncloc 1089127

关于java - 无法使用 gson 库反序列化来自 SonarQube 的 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42929568/

相关文章:

c++ - 如何使用 Sonar C/C++ 插件激活 cppcheck 规则

intellij-idea - IntelliJ 上 SonarLint v2.0 的连接模式不起作用?

java - 使用 GSON 按变量键对 JsonArray 进行排序

java - Gson/Libgdx Json : Serialize class that extends ArrayList

java - 从临时目录打开 InputStream 时出错

java - 陈旧元素引用 : element is not attached to the page document for Java-Selenium

java - 面板的绘制组件不绘制双点值

java - 运行 JavaFX 应用程序时发生 fatal error JRE(调整大小/场景更改)

jenkins - Jenkins 2.7.1 中没有 Sonarqube 扫描仪配置

java - 使用 Google 的 Gson 反序列化 Bugzilla JSON 时出现问题