开发者

How to create a java class for a JSON string where keys are variable?

The response

array (
    0 => 
    array (
      'time_start' => 1252652400,
      'time_stop' => 1252911600,
      'stats' => 
      array (
        6002306163363 => 
        array (
          'id' => 6002306163363,
          'impressions' => '6713',
          'clicks' => '7',
          'spent' => '593',
          'actions' => '1',
        ),
      ),
    ),
  )

data is shown in facebook api of rest/ads.getAdGroupStats.

I am not able to convert the stats part to a Java class, Where the 6002306163363 is a variable and similarly could have many more mappings. Below is the full result for three ads 123456,23456,34567.

[
  {
    "time_start": 0,
    "time_stop": 1285224928,
    "stats": {
      "123456": {
        "id": 123456,
        "impressions": 40,
        "clicks": 0,
        "spent": 0,
        "social_impressions": 0,
        "social_clicks": 0,
        "social_spent": 0
      },
      "23456": {
        "id": 23456,
        "impressions": 3,
        "clicks": 0,
        "spent": 0,
        "social_impressions": 0,
        "social_clicks": 0,
        "social_spent": 0
      },
      "34567": {
        "id": 34567,
        "impressions": 211457,
        "clicks": 84,
        "spent": 6898,
        "social_impressions": 124,
        "social_clicks": 0,
        "social_spent": 0
      }
    }
  }
]

I have to make a Java class which could map to the above JSON and not able to do so. Can anyone please help me here?

Update : I am getting this data from facebook and in the api that we are using requires class, so that the returned json could be mapped. I have only control to create a cl开发者_Go百科ass so that api internally map this out. I need the format of the java class required.


You need a hashmap or something similar to deal with those numerical keys.

public class GroupStats {
   long time_start;
   long time_stop;
   HashMap<GroupAccount> stats;
}

public class GroupAccount {
   long id;
   int impressions;
   int clicks;
   int spent;
   int social_impressions;
   int social_spent;
}


I'm Mark Allen (RestFB maintainer). Version 1.6 should address this with the new option to map to the built-in JsonObject type - check out http://restfb.com for an example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜