Java Can';t从Flurry API反序列化json响应

Java Can';t从Flurry API反序列化json响应,java,json,spring,flurry,Java,Json,Spring,Flurry,我试图在我的Spring应用程序中使用Flurry的RESTAPI读取一些统计数据。但是,我的对象返回时所有字段的值都为空。我认为这是因为JSON响应将@symbols前置到响应中的非数组字段。我得到的答复如下: { "@endDate": "2015-06-16", "@metric": "PageViews", "@startDate": "2015-06-16", "@generatedDate": "6/16/15 5:06 PM", "@version": "1.0

我试图在我的Spring应用程序中使用Flurry的RESTAPI读取一些统计数据。但是,我的对象返回时所有字段的值都为空。我认为这是因为JSON响应将@symbols前置到响应中的非数组字段。我得到的答复如下:

{
  "@endDate": "2015-06-16",
  "@metric": "PageViews",
  "@startDate": "2015-06-16",
  "@generatedDate": "6/16/15 5:06 PM",
  "@version": "1.0",
  "day": [
          {
            "@date": "2015-06-16",
            "@value": "0"
          }
         ]
}
RestTemplate restTemplate = new RestTemplate();
FlurryAppMetric appMetric = restTemplate.getForObject(url, FlurryAppMetric.class);
return appMetric;
public class FlurryAppMetric {
    @JsonProperty("@metric")
    private String metric;

    @JsonProperty("@startDate")
    private String startDate;

    @JsonProperty("@endDate")
    private String endDate;

    @JsonProperty("@generatedDate")
    private String generatedDate;

    @JsonProperty("@version")
    private String version;

    private ArrayList<FlurryMetric> day;
}

public class FlurryMetric {
    @JsonProperty("@date")
    private String date;

    @JsonProperty("@value")
    private String value;
}
我用于发出请求的代码如下所示:

{
  "@endDate": "2015-06-16",
  "@metric": "PageViews",
  "@startDate": "2015-06-16",
  "@generatedDate": "6/16/15 5:06 PM",
  "@version": "1.0",
  "day": [
          {
            "@date": "2015-06-16",
            "@value": "0"
          }
         ]
}
RestTemplate restTemplate = new RestTemplate();
FlurryAppMetric appMetric = restTemplate.getForObject(url, FlurryAppMetric.class);
return appMetric;
public class FlurryAppMetric {
    @JsonProperty("@metric")
    private String metric;

    @JsonProperty("@startDate")
    private String startDate;

    @JsonProperty("@endDate")
    private String endDate;

    @JsonProperty("@generatedDate")
    private String generatedDate;

    @JsonProperty("@version")
    private String version;

    private ArrayList<FlurryMetric> day;
}

public class FlurryMetric {
    @JsonProperty("@date")
    private String date;

    @JsonProperty("@value")
    private String value;
}
其中FlurryAppMetric如下(省略了getter和setter):

公共类FlurryAppMetric{
私有字符串度量;
私有字符串起始日期;
私有字符串结束日期;
私有字符串生成日期;
私有字符串版本;
私人ArrayList日;
}
公共级流速仪{
私有字符串日期;
私有字符串值;
}
一个可能的解决方案是将其全部解析为一个映射,但如果可能的话,我想利用它们公开的映射器


如果有办法发出GET请求并将正文作为字符串接收,我将能够清理响应并尝试将其传递给映射器。

您应该能够使用GSON解析它,使用
@SerializedName
注释,如下所示:

public class FlurryAppMetric {
    @SerializedName("@metric");
    private String metric;

    @SerializedName("@startDate");
    private String startDate;

    @SerializedName("@endDate");
    private String endDate;

    @SerializedName("@generatedDate");
    private String generatedDate;

    @SerializedName("@versionDate");
    private String version;

    @SerializedName("day");
    private ArrayList<FlurryMetric> day;
}

public class FlurryMetric {
    @SerializedName("@date");
    private String date;

    @SerializedName("@value");
    private String value;
}
    Gson gson = new Gson();
    gson.fromJson(<string json source>, FlurryApiMetric.class);
公共类FlurryAppMetric{
@序列化名称(“@metric”);
私有字符串度量;
@序列化名称(“@startDate”);
私有字符串起始日期;
@序列化名称(“@endDate”);
私有字符串结束日期;
@序列化名称(“@generatedDate”);
私有字符串生成日期;
@序列化名称(“@versionDate”);
私有字符串版本;
@序列化名称(“日期”);
私人ArrayList日;
}
公共级流速仪{
@序列化名称(“日期”);
私有字符串日期;
@序列化名称(“@value”);
私有字符串值;
}
然后像这样使用Gson:

public class FlurryAppMetric {
    @SerializedName("@metric");
    private String metric;

    @SerializedName("@startDate");
    private String startDate;

    @SerializedName("@endDate");
    private String endDate;

    @SerializedName("@generatedDate");
    private String generatedDate;

    @SerializedName("@versionDate");
    private String version;

    @SerializedName("day");
    private ArrayList<FlurryMetric> day;
}

public class FlurryMetric {
    @SerializedName("@date");
    private String date;

    @SerializedName("@value");
    private String value;
}
    Gson gson = new Gson();
    gson.fromJson(<string json source>, FlurryApiMetric.class);
Gson-Gson=new-Gson();
fromJson(,FlurryApiMetric.class);

reidzeibel让我走上了正确的道路。我使用Jackson FasterXML解析器,因为org.springframework.web.client.RestTemplate在幕后使用了它。与GSON库的@SerializedName类似,Jackson库提供了@JsonProperty成员属性。生成的模型类如下所示:

{
  "@endDate": "2015-06-16",
  "@metric": "PageViews",
  "@startDate": "2015-06-16",
  "@generatedDate": "6/16/15 5:06 PM",
  "@version": "1.0",
  "day": [
          {
            "@date": "2015-06-16",
            "@value": "0"
          }
         ]
}
RestTemplate restTemplate = new RestTemplate();
FlurryAppMetric appMetric = restTemplate.getForObject(url, FlurryAppMetric.class);
return appMetric;
public class FlurryAppMetric {
    @JsonProperty("@metric")
    private String metric;

    @JsonProperty("@startDate")
    private String startDate;

    @JsonProperty("@endDate")
    private String endDate;

    @JsonProperty("@generatedDate")
    private String generatedDate;

    @JsonProperty("@version")
    private String version;

    private ArrayList<FlurryMetric> day;
}

public class FlurryMetric {
    @JsonProperty("@date")
    private String date;

    @JsonProperty("@value")
    private String value;
}
公共类FlurryAppMetric{
@JsonProperty(“@metric”)
私有字符串度量;
@JsonProperty(“@startDate”)
私有字符串起始日期;
@JsonProperty(“@endDate”)
私有字符串结束日期;
@JsonProperty(“@generatedDate”)
私有字符串生成日期;
@JsonProperty(“版本”)
私有字符串版本;
私人ArrayList日;
}
公共级流速仪{
@JsonProperty(“日期”)
私有字符串日期;
@JsonProperty(“@value”)
私有字符串值;
}

很高兴知道,这样我就不需要操纵JSON字符串了。我仍然不确定应该使用哪个类在Spring中发出GET请求,以获取作为字符串的响应体。如果Flurry使用GSON,您可以将注释添加到模型中,并让FlurryAppMetric实现保持原样:DGlad it works!事实上,我也经常使用Jackson,在GSON上回答是因为改型使用它:DRight,我只使用Jackson,因为Spring提供的REST客户端类在封面下使用它D