无法使用GSON将复杂的json序列化为对象?

时间:2014-06-20 03:00:53

标签: java json serialization gson

下面是我的JSON字符串,我通过从服务API调用回来。为了理解目的,我只有三个reportRecords缩短了它。一般来说,它可能有~500 reportRecords

{
   "aggRecords": {
      "reportRecords": [
         {
            "min": 0,
            "max": 12,
            "avg": 0.3699187,
            "count": 246,
            "sumSq": 571,
            "stddev": 1.4779372,
            "median": 0,
            "percentileMap": {
               "95": 4
            },
            "metricName": "TransactionDuration",
            "dimensions": {
               "env": "dev",
               "pool": "titan",
               "Name": "PostProcessing",
               "Type": "PostProcessing"
            },
            "value": 91
         },
         {
            "min": 0,
            "max": 23,
            "avg": 2.3991289E-4,
            "count": 1463031,
            "sumSq": 3071,
            "stddev": 0.045814946,
            "median": 0,
            "percentileMap": {
               "95": 0
            },
            "metricName": "TransactionDuration",
            "dimensions": {
               "env": "dev",
               "pool": "titan",
               "Name": "ResourceContext",
               "Type": "ResourceContext"
            },
            "value": 351
         },
         {
            "min": 0,
            "max": 1209,
            "avg": 1.9203402,
            "count": 7344636,
            "sumSq": 71832774,
            "stddev": 2.4683187,
            "median": 2,
            "percentileMap": {
               "95": 4
            },
            "metricName": "TransactionDuration",
            "dimensions": {
               "env": "dev",
               "pool": "titan",
               "Name": "Client::Sync",
               "Type": "Client::Sync"
            },
            "value": 14104200
         }
      ]
   },
   "minRecordsMap": {}
}

现在,从上面的JSON响应中,我需要提取reportRecordsName的{​​{1}}。这意味着,我只需从上面的JSON响应中提取Client::Sync以下。

reportRecords

现在我需要将 { "min": 0, "max": 1209, "avg": 1.9203402, "count": 7344636, "sumSq": 71832774, "stddev": 2.4683187, "median": 2, "percentileMap": { "95": 4 }, "metricName": "TransactionDuration", "dimensions": { "env": "dev", "pool": "titan", "Name": "Client::Sync", "Type": "Client::Sync" }, "value": 14104200 } 上面的reportRecords解析为以下对象 -

Client::Sync

所以在序列化之后,上面的对象应该是这样的 -

public class DataMetrics {

    private String pool;
    private String name;
    private String type;
    private String env;
    private String metricName;
    private String percentile;
    private String median;
    private String stdDev;
    private String sumSq;
    private String count;
    private String avg;
    private String max;
    private String min;

    // getters and setters here
}

我在这里和下面使用GSON库是我到目前为止所尝试的 -

pool is titan
name is Client::Sync 
type is Client::Sync
env is dev
metricNname is TransactionDuration
95th  percentile is 4
median is 2
stdDev is 2.4683187 
sumSq is 71832774 
count is 7344636 
avg is 1.9203402
max is 1209
min is 0

但是出于某种原因,DataMetrics中的某些字段向我显示了数据,但有些字段显示为空,这使我觉得上面的代码中有问题或者我的DataMetrics类不正确?

1 个答案:

答案 0 :(得分:1)

通常,对于像dimensions这样的嵌套对象,您将为其声明另一个POJO

class DataMetrics{
  private String min;
  private String max;
  private String avg;
  /*Insert other members*/

  private Dimensions dimensions;

  public boolean isClientSync(){
    return "Client::Sync".equals(dimensions.Name);
  }
  private class Dimensions{
    private String env;
    private String pool;
    private String Name;
  }
}

测试:

 public void test() {

 String json = "[{\"min\": 0,\"max\": 1209,\"avg\": 1.9203402,\"count\": 7344636,\"sumSq\": 71832774,  \"stddev\": 2.4683187, \"median\": 2,\"percentileMap\": {\"95\": 4},\"metricName\": \"TransactionDuration\",\"dimensions\": {\"env\": \"dev\",\"pool\": \"titan\",\"Name\": \"Client::Sync\", \"Type\": \"Client::Sync\"},\"value\": 14104200}]";

 final Gson gson = new Gson();
 final Type type = new TypeToken<List<DataMetrics>>() {}.getType();
 final List<DataMetrics> records = gson.fromJson(json, type);

 Assert.assertTrue(records.get(0).isClientSync());
}

对于percentileMap,您需要一个实际的地图

private Map<String, Integer> percentileMap;

另请注意,成员变量名称应与JSON属性名称完全匹配

  • stdDev =&gt; stddev
  • name =&gt; Name
  • percentile =&gt; percentileMap