List of usage examples for com.google.gson JsonObject has
public boolean has(String memberName)
From source file:com.facebook.ads.sdk.CheckBatchRequestStatus.java
License:Open Source License
public static APINodeList<CheckBatchRequestStatus> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<CheckBatchRequestStatus> checkBatchRequestStatuss = new APINodeList<CheckBatchRequestStatus>( request, json);//from ww w . j a v a 2 s .c o m JsonArray arr; JsonObject obj; JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { checkBatchRequestStatuss.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return checkBatchRequestStatuss; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; checkBatchRequestStatuss.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { checkBatchRequestStatuss .add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { checkBatchRequestStatuss.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { checkBatchRequestStatuss.add(loadJSON(obj.toString(), context)); } } return checkBatchRequestStatuss; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { checkBatchRequestStatuss.add(loadJSON(entry.getValue().toString(), context)); } return checkBatchRequestStatuss; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { checkBatchRequestStatuss.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return checkBatchRequestStatuss; } // Sixth, check if it's pure JsonObject checkBatchRequestStatuss.clear(); checkBatchRequestStatuss.add(loadJSON(json, context)); return checkBatchRequestStatuss; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.ConnectionObject.java
License:Open Source License
public static APINodeList<ConnectionObject> parseResponse(String json, APIContext context, APIRequest request) { APINodeList<ConnectionObject> connectionObjects = new APINodeList<ConnectionObject>(request, json); JsonArray arr;//from w w w. j ava 2 s . c o m JsonObject obj; JsonParser parser = new JsonParser(); try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { connectionObjects.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return connectionObjects; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { try { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); connectionObjects.setPaging(paging.get("before").getAsString(), paging.get("after").getAsString()); } catch (Exception ignored) { } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { connectionObjects.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); connectionObjects.add(loadJSON(obj.toString(), context)); } return connectionObjects; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { connectionObjects.add(loadJSON(entry.getValue().toString(), context)); } return connectionObjects; } else { // Fifth, check if it's pure JsonObject connectionObjects.add(loadJSON(json, context)); return connectionObjects; } } } catch (Exception e) { } return null; }
From source file:com.facebook.ads.sdk.ConnectionObjectOpenGraphAction.java
License:Open Source License
public static APINodeList<ConnectionObjectOpenGraphAction> parseResponse(String json, APIContext context, APIRequest request) {/*w w w . j av a2s . co m*/ APINodeList<ConnectionObjectOpenGraphAction> connectionObjectOpenGraphActions = new APINodeList<ConnectionObjectOpenGraphAction>( request, json); JsonArray arr; JsonObject obj; JsonParser parser = new JsonParser(); try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { connectionObjectOpenGraphActions .add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return connectionObjectOpenGraphActions; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { try { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); connectionObjectOpenGraphActions.setPaging(paging.get("before").getAsString(), paging.get("after").getAsString()); } catch (Exception ignored) { } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { connectionObjectOpenGraphActions .add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); connectionObjectOpenGraphActions.add(loadJSON(obj.toString(), context)); } return connectionObjectOpenGraphActions; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { connectionObjectOpenGraphActions.add(loadJSON(entry.getValue().toString(), context)); } return connectionObjectOpenGraphActions; } else { // Fifth, check if it's pure JsonObject connectionObjectOpenGraphActions.add(loadJSON(json, context)); return connectionObjectOpenGraphActions; } } } catch (Exception e) { } return null; }
From source file:com.facebook.ads.sdk.ConnectionObjectOpenGraphObject.java
License:Open Source License
public static APINodeList<ConnectionObjectOpenGraphObject> parseResponse(String json, APIContext context, APIRequest request) {//from w w w .java 2 s . c o m APINodeList<ConnectionObjectOpenGraphObject> connectionObjectOpenGraphObjects = new APINodeList<ConnectionObjectOpenGraphObject>( request, json); JsonArray arr; JsonObject obj; JsonParser parser = new JsonParser(); try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { connectionObjectOpenGraphObjects .add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return connectionObjectOpenGraphObjects; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { try { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); connectionObjectOpenGraphObjects.setPaging(paging.get("before").getAsString(), paging.get("after").getAsString()); } catch (Exception ignored) { } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { connectionObjectOpenGraphObjects .add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); connectionObjectOpenGraphObjects.add(loadJSON(obj.toString(), context)); } return connectionObjectOpenGraphObjects; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { connectionObjectOpenGraphObjects.add(loadJSON(entry.getValue().toString(), context)); } return connectionObjectOpenGraphObjects; } else { // Fifth, check if it's pure JsonObject connectionObjectOpenGraphObjects.add(loadJSON(json, context)); return connectionObjectOpenGraphObjects; } } } catch (Exception e) { } return null; }
From source file:com.facebook.ads.sdk.ConnectionObjectOpenGraphObjectProperty.java
License:Open Source License
public static APINodeList<ConnectionObjectOpenGraphObjectProperty> parseResponse(String json, APIContext context, APIRequest request) { APINodeList<ConnectionObjectOpenGraphObjectProperty> connectionObjectOpenGraphObjectPropertys = new APINodeList<ConnectionObjectOpenGraphObjectProperty>( request, json);/*from w ww.j a v a2s . c om*/ JsonArray arr; JsonObject obj; JsonParser parser = new JsonParser(); try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { connectionObjectOpenGraphObjectPropertys .add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return connectionObjectOpenGraphObjectPropertys; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { try { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); connectionObjectOpenGraphObjectPropertys.setPaging(paging.get("before").getAsString(), paging.get("after").getAsString()); } catch (Exception ignored) { } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { connectionObjectOpenGraphObjectPropertys .add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); connectionObjectOpenGraphObjectPropertys.add(loadJSON(obj.toString(), context)); } return connectionObjectOpenGraphObjectPropertys; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { connectionObjectOpenGraphObjectPropertys .add(loadJSON(entry.getValue().toString(), context)); } return connectionObjectOpenGraphObjectPropertys; } else { // Fifth, check if it's pure JsonObject connectionObjectOpenGraphObjectPropertys.add(loadJSON(json, context)); return connectionObjectOpenGraphObjectPropertys; } } } catch (Exception e) { } return null; }
From source file:com.facebook.ads.sdk.ConversionActionQuery.java
License:Open Source License
public static APINodeList<ConversionActionQuery> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<ConversionActionQuery> conversionActionQuerys = new APINodeList<ConversionActionQuery>(request, json);/* w w w. j av a 2 s. c om*/ JsonArray arr; JsonObject obj; JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { conversionActionQuerys.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return conversionActionQuerys; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; conversionActionQuerys.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { conversionActionQuerys.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { conversionActionQuerys.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { conversionActionQuerys.add(loadJSON(obj.toString(), context)); } } return conversionActionQuerys; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { conversionActionQuerys.add(loadJSON(entry.getValue().toString(), context)); } return conversionActionQuerys; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { conversionActionQuerys.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return conversionActionQuerys; } // Sixth, check if it's pure JsonObject conversionActionQuerys.clear(); conversionActionQuerys.add(loadJSON(json, context)); return conversionActionQuerys; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.CustomAudience.java
License:Open Source License
public static APINodeList<CustomAudience> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<CustomAudience> customAudiences = new APINodeList<CustomAudience>(request, json); JsonArray arr;//from w ww . j av a 2 s .c o m JsonObject obj; JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { customAudiences.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return customAudiences; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; customAudiences.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { customAudiences.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { customAudiences.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { customAudiences.add(loadJSON(obj.toString(), context)); } } return customAudiences; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { customAudiences.add(loadJSON(entry.getValue().toString(), context)); } return customAudiences; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { customAudiences.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return customAudiences; } // Sixth, check if it's pure JsonObject customAudiences.clear(); customAudiences.add(loadJSON(json, context)); return customAudiences; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.CustomAudienceAdAccount.java
License:Open Source License
public static APINodeList<CustomAudienceAdAccount> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<CustomAudienceAdAccount> customAudienceAdAccounts = new APINodeList<CustomAudienceAdAccount>( request, json);//from w w w . ja v a 2 s . c o m JsonArray arr; JsonObject obj; JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { customAudienceAdAccounts.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return customAudienceAdAccounts; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; customAudienceAdAccounts.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { customAudienceAdAccounts .add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { customAudienceAdAccounts.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { customAudienceAdAccounts.add(loadJSON(obj.toString(), context)); } } return customAudienceAdAccounts; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { customAudienceAdAccounts.add(loadJSON(entry.getValue().toString(), context)); } return customAudienceAdAccounts; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { customAudienceAdAccounts.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return customAudienceAdAccounts; } // Sixth, check if it's pure JsonObject customAudienceAdAccounts.clear(); customAudienceAdAccounts.add(loadJSON(json, context)); return customAudienceAdAccounts; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.CustomAudienceCapabilities.java
License:Open Source License
public static APINodeList<CustomAudienceCapabilities> parseResponse(String json, APIContext context, APIRequest request) {/*w ww . j a v a 2 s. co m*/ APINodeList<CustomAudienceCapabilities> customAudienceCapabilitiess = new APINodeList<CustomAudienceCapabilities>( request, json); JsonArray arr; JsonObject obj; JsonParser parser = new JsonParser(); try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { customAudienceCapabilitiess.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return customAudienceCapabilitiess; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { try { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); customAudienceCapabilitiess.setPaging(paging.get("before").getAsString(), paging.get("after").getAsString()); } catch (Exception ignored) { } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { customAudienceCapabilitiess .add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); customAudienceCapabilitiess.add(loadJSON(obj.toString(), context)); } return customAudienceCapabilitiess; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { customAudienceCapabilitiess.add(loadJSON(entry.getValue().toString(), context)); } return customAudienceCapabilitiess; } else { // Fifth, check if it's pure JsonObject customAudienceCapabilitiess.add(loadJSON(json, context)); return customAudienceCapabilitiess; } } } catch (Exception e) { } return null; }
From source file:com.facebook.ads.sdk.CustomAudienceDataSource.java
License:Open Source License
public static APINodeList<CustomAudienceDataSource> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<CustomAudienceDataSource> customAudienceDataSources = new APINodeList<CustomAudienceDataSource>( request, json);/*from ww w .ja v a 2s . c o m*/ JsonArray arr; JsonObject obj; JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { customAudienceDataSources.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return customAudienceDataSources; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; customAudienceDataSources.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { customAudienceDataSources .add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { customAudienceDataSources.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { customAudienceDataSources.add(loadJSON(obj.toString(), context)); } } return customAudienceDataSources; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { customAudienceDataSources.add(loadJSON(entry.getValue().toString(), context)); } return customAudienceDataSources; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { customAudienceDataSources.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return customAudienceDataSources; } // Sixth, check if it's pure JsonObject customAudienceDataSources.clear(); customAudienceDataSources.add(loadJSON(json, context)); return customAudienceDataSources; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }