Json response i got from facebook API. Used wrapper class to deserialize.
{
"accounts" : {
"data" : [{
"picture" : {
"data" : {
"is_silhouette" : true,
"url" : "https://scontent.xx.fbcdn.net/v/t1.0-1/c15.0.50.50/p50x50/405770_10150000700901972_765915345_n.png?oh=b9dfdfgg40ebc96d0992e27255ccc5a16d678&oe=58F973E1"
}
},
"id" : "746973423232342126509",
"name" : "Sneakpick"
}
],
"paging" : {
"cursors" : {
"before" : "NzQ2OTczNfdgfDQyMTI2NTA5",
"after" : "MTYxNjM2NdfgfzE3ODY1ODk5MQZDZD"
}
}
},
"id" : "115020323171435432395"
}
Wrapper class created using https://json2apex.herokuapp.com/
global class facebookpages {
global facebookpages()
{
}
global Accounts accounts {get;set;}
global String id {get;set;}
// inner class level 1 accounts
global class Accounts {
global List<Data_Z> data {get;set;}
global Paging paging {get;set;}
global Accounts(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'data') {
data = new List<Data_Z>();
while (parser.nextToken() != JSONToken.END_ARRAY) {
data.add(new Data_Z(parser));
}
} else if (text == 'paging') {
paging = new Paging(parser);
} else {
System.debug(LoggingLevel.WARN, 'Accounts consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
// inner class level 1 paging
global class Paging {
global Cursors cursors {get;set;}
global Paging(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'cursors') {
cursors = new Cursors(parser);
} else {
System.debug(LoggingLevel.WARN, 'Paging consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
// inner class level 2 Cursors
global class Cursors {
global String before {get;set;}
global String after {get;set;}
global Cursors(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'before') {
before = parser.getText();
} else if (text == 'after') {
after = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'Cursors consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
// inner classes level 2 Data_Z
global class Data_Z {
global Picture picture {get;set;}
global String name {get;set;}
global String id {get;set;}
global Data_Z(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'picture') {
picture = new Picture(parser);
} else if (text == 'name') {
name = parser.getText();
} else if (text == 'id') {
id = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'Data_Z consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
// innner class level 3 picture
global class Picture {
global Data data {get;set;}
global Picture(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'data') {
data = new Data(parser);
} else {
System.debug(LoggingLevel.WARN, 'Picture consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
// inner class level 4 data
global class Data {
global Boolean is_silhouette {get;set;}
global String url {get;set;}
global Data(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'is_silhouette') {
is_silhouette = parser.getBooleanValue();
} else if (text == 'url') {
url = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'Data consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
}
// level 0 main class methods and properties
global static void consumeObject(JSONParser parser) {
Integer depth = 0;
do {
JSONToken curr = parser.getCurrentToken();
if (curr == JSONToken.START_OBJECT ||
curr == JSONToken.START_ARRAY) {
depth++;
} else if (curr == JSONToken.END_OBJECT ||
curr == JSONToken.END_ARRAY) {
depth--;
}
} while (depth > 0 && parser.nextToken() != null);
}
global facebookpages(JSONParser parser) {
while (parser.nextToken() != JSONToken.END_OBJECT) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
if (parser.nextToken() != JSONToken.VALUE_NULL) {
if (text == 'accounts') {
accounts = new Accounts(parser);
} else if (text == 'id') {
id = parser.getText();
} else {
System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
consumeObject(parser);
}
}
}
}
}
global static facebookpages parse(String json) {
return new facebookpages(System.JSON.createParser(json));
}
}
Debug log after this i am getting data in this class object that is perfect.
|DEBUG|facebookpages:[accounts=Accounts:[data=(Data_Z:[id=746973509, name=Sneakpick, picture=Picture:[data=Data:[is_silhouette=true, url=scontent.xx.fbcdn.net/v/t1.0-1/c15.0.50.50/p50x50/…, Data_Z:[id=1616367158991, name=Busyday, picture=Picture:[data=Data:[is_silhouette=true, url=scontent.xx.fbcdn.net/v/t1.0-1/c15.0.50.50/p50x50/…), paging=Paging:[cursors=Cursors:[after=MTYxNjM2NzE3Y1ODMQZDZD, before=NzQ2OTczNDTI2A5]]], id=11470363662]
I want to insert these pages names and ID using batch apex. tried as below. stucked in Start method what to return and is there require to fire query ?.
global class addfbpagestosystem implements Database.Batchable<sObject>
{
global final list<facebookpages.Data_Z> fbpages;
global final String id;
global addfbpagestosystem(list<facebookpages.Data_Z> fbpages,String id)
{
this.id =id;
this.fbpages=fbpages;
}
global Iterable<sObject> start(Database.BatchableContext BC)
{ // stucked in this method.
/* MBT_SFFB__Facebook_Page__c objfbpage = new MBT_SFFB__Facebook_Page__c()
return MBT_SFFB__Facebook_Page__c;*/
}
global void execute(Database.BatchableContext BC,List<sObject> scope)
{
list<MBT_SFFB__Facebook_Page__c> objmtfbpage = new list<MBT_SFFB__Facebook_Page__c>();
for(facebookpages.Data_Z objfbpage :fbpages)
{
MBT_SFFB__Facebook_Page__c objrecfbpage = new MBT_SFFB__Facebook_Page__c();
objrecfbpage.MBT_SFFB__Facebook_ID__c=id;
objrecfbpage.MBT_SFFB__Facebook_Page_ID__c=objfbpage.id;
objrecfbpage.MBT_SFFB__Facebook_Page_Name__c=objfbpage.name;
objrecfbpage.MBT_SFFB__Image__c=objfbpage.picture.data.url;
objrecfbpage.MBT_SFFB__Facebook_User_Relates_To_Page__c='a0H2800000B6nR0';
objmtfbpage.add(objrecfbpage);
}
insert objmtfbpage;
}
global void finish(Database.BatchableContext BC)
{
}
}