我想将JSONArray映射到对象List。 这是我的数据看起来像
{"data":[{"campaign_id":"23842660156110613","campaign_name":"LXR Guide_Lead Generation_Landing Page","clicks":"20","cpc":"0.815","ctr":"0.55417","impressions":"3609","objective":"LINK_CLICKS","reach":"2331","unique_clicks":"18","social_spend":"0.07","spend":"16.3","unique_ctr":"0.772201","inline_post_engagement":"19","date_start":"2017-10-12","date_stop":"2017-10-12","account_id":"405997239770197","actions":[{"action_type":"link_click","value":"19"},{"action_type":"post_reaction","value":"1"},{"action_type":"page_engagement","value":"20"},{"action_type":"post_engagement","value":"20"}]},
{"campaign_id":"23842587341440613","campaign_name":"Post: \"Join us on May 12 at 2 p.m. as we explore how you...\"","clicks":"2","cpc":"3.44","ctr":"2.597403","impressions":"77","objective":"POST_ENGAGEMENT","reach":"77","unique_clicks":"2","social_spend":"0","spend":"6.88","unique_ctr":"2.597403","inline_post_engagement":"2","date_start":"2017-05-12","date_stop":"2017-05-12","account_id":"405997239770197","actions":[{"action_type":"link_click","value":"2"},{"action_type":"post_reaction","value":"31"},{"action_type":"page_engagement","value":"33"},{"action_type":"post_engagement","value":"33"}]}],"paging":{"cursors":{"before":"MAZDZD","after":"NjMZD"}}}
我的代码是
JSONArray dataList = advertIdObj.getJSONArray("data");
ObjectMapper mapper = new ObjectMapper();
campaignList = mapper.readValue(dataList.toString(), new TypeReference<List<FacebookCamapignStats>>() {
});
我的FacebookCamapignStats.java类:
@Column(name = "IMPRESSIONS")
protected int impressions;
@Column(name = "CLICKS")
protected long clicks;
@Column(name = "COST_PER_PURCHASE")
protected float cpp;
@Column(name = "SPEND")
protected float spend;
@Column(name = "FACEBOOK_ORDERS")
protected int facebookOrders;
@Column(name = "FACEBOOK_REVENUE")
protected float facebookRevenue;
@JsonProperty(value = "post_engagement")
@Column(name = "POST_ENGAGEMENT")
protected int postEngagement;
@JsonProperty(value = "like")
@Column(name = "PAGE_LIKES")
protected int pageLikes;
// getters and setters
我想将dataList的所有值映射到广告系列列表
答案 0 :(得分:0)
这里是Jackson
- 对象应该是什么样子:
public class FacebookCampaignStats {
public FacebookCampaignStats() {}
protected List<Data> data;
protected Paging paging;
static class Data {
public Data() {}
@JsonProperty("campaign_id")
protected String campaignId;
@JsonProperty("campaign_name")
protected String campaignName;
protected String impressions;
protected String clicks;
protected String ctr;
protected String cpc;
protected String objective;
protected String reach;
@JsonProperty(value = "unique_clicks")
protected String uniqueClicks;
@JsonProperty(value = "social_spend")
protected String socialSpend;
@JsonProperty(value = "unique_ctr")
protected String uniqueCtr;
@JsonProperty(value = "inline_post_engagement")
protected String inPostEng;
@JsonProperty(value = "date_start")
protected String dateStart;
@JsonProperty(value = "date_stop")
protected String dateStop;
@JsonProperty(value = "account_id")
protected String accountId;
@JsonProperty(value = "actions")
protected List<Action> actions;
protected String cpp;
protected String spend;
protected String facebookOrders;
protected String facebookRevenue;
@JsonProperty(value = "post_engagement")
protected String postEngagement;
@JsonProperty(value = "like")
protected String pageLikes;
static class Action {
public Action() {}
@JsonProperty("action_type")
private String actionType;
@JsonProperty("value")
private String value;
}
}
static class Paging {
public Paging() {}
private Cursors cursors;
class Cursors {
public Cursors() {}
private String before;
private String after;
}
}
}
我使用以下代码测试了您的示例:
public static void main(String[] args) {
String dataList = "{\"data\":[{\"campaign_id\":\"23842660156110613\",\"campaign_name\":\"LXR Guide_Lead Generation_Landing Page\",\"clicks\":\"20\",\"cpc\":\"0.815\",\"ctr\":\"0.55417\",\"impressions\":\"3609\",\"objective\":\"LINK_CLICKS\",\"reach\":\"2331\",\"unique_clicks\":\"18\",\"social_spend\":\"0.07\",\"spend\":\"16.3\",\"unique_ctr\":\"0.772201\",\"inline_post_engagement\":\"19\",\"date_start\":\"2017-10-12\",\"date_stop\":\"2017-10-12\",\"account_id\":\"405997239770197\",\"actions\":[{\"action_type\":\"link_click\",\"value\":\"19\"},{\"action_type\":\"post_reaction\",\"value\":\"1\"},{\"action_type\":\"page_engagement\",\"value\":\"20\"},{\"action_type\":\"post_engagement\",\"value\":\"20\"}]}," +
"{\"campaign_id\":\"23842587341440613\",\"campaign_name\":\"Post: \\\"Join us on May 12 at 2 p.m. as we explore how you...\\\"\",\"clicks\":\"2\",\"cpc\":\"3.44\",\"ctr\":\"2.597403\",\"impressions\":\"77\",\"objective\":\"POST_ENGAGEMENT\",\"reach\":\"77\",\"unique_clicks\":\"2\",\"social_spend\":\"0\",\"spend\":\"6.88\",\"unique_ctr\":\"2.597403\",\"inline_post_engagement\":\"2\",\"date_start\":\"2017-05-12\",\"date_stop\":\"2017-05-12\",\"account_id\":\"405997239770197\",\"actions\":[{\"action_type\":\"link_click\",\"value\":\"2\"},{\"action_type\":\"post_reaction\",\"value\":\"31\"},{\"action_type\":\"page_engagement\",\"value\":\"33\"},{\"action_type\":\"post_engagement\",\"value\":\"33\"}]}],\"paging\":{\"cursors\":{\"before\":\"MAZDZD\",\"after\":\"NjMZD\"}}}";
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
try {
FacebookCampaignStats campaignList = mapper.readValue(dataList.toString(), FacebookCampaignStats.class);
System.out.println(mapper.writeValueAsString(campaignList));
} catch (Exception e) {
e.printStackTrace();
}
}