views:

64

answers:

1

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 class so that api internally map this out. I need the format of the java class required.

+1  A: 

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;
}
ammoQ
So internal api would automatically map this Object(GroupAccount) to the string "123456"?
DKSRathore
Also, I have tried this already but have not worked.
DKSRathore
Of course you need a json parser and some work to get the JSON data into the classes described above, that work won't do itself automagically.
ammoQ
@ammoQ I am using restfb jar and where Parser is included in the 3rd party jar and api asks for Mapping class. So should I modify this jar? I mean everytime I update the jar to new version, I'll need to update the parser.
DKSRathore
I don't know this jar, but I guess it's not clever enough to do a mapping like that. Doing the mapping by hand should be pretty straight-forward using e.g. the json parser from json.org.
ammoQ