简体   繁体   English

如何将JSONArray映射到对象列表

[英]How to map JSONArray to Object List

I want to map JSONArray to object List. 我想将JSONArray映射到对象列表。 Here is my data look like 这是我的数据

{"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"}}}

my code is 我的代码是

JSONArray dataList = advertIdObj.getJSONArray("data");
            ObjectMapper mapper = new ObjectMapper();
            campaignList = mapper.readValue(dataList.toString(), new TypeReference<List<FacebookCamapignStats>>() {
            });

My FacebookCamapignStats.java class: 我的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 //获取器和设置器

I want to map all the values of dataList to campaign List 我想将dataList的所有值映射到广告系列列表

Here's what the Jackson -ized object should look like: 这是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;
        }

    }
}

And I tested your example with the following code: 我用以下代码测试了您的示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM