List of usage examples for com.google.gson JsonElement getAsJsonArray
public JsonArray getAsJsonArray()
From source file:com.facebook.ads.sdk.CustomAudiencesTOS.java
License:Open Source License
public static APINodeList<CustomAudiencesTOS> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<CustomAudiencesTOS> customAudiencesTOSs = new APINodeList<CustomAudiencesTOS>(request, json); JsonArray arr;//w w w .ja va 2s. co 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++) { customAudiencesTOSs.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return customAudiencesTOSs; } 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; customAudiencesTOSs.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++) { customAudiencesTOSs.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()) { customAudiencesTOSs.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { customAudiencesTOSs.add(loadJSON(obj.toString(), context)); } } return customAudiencesTOSs; } 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()) { customAudiencesTOSs.add(loadJSON(entry.getValue().toString(), context)); } return customAudiencesTOSs; } 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)) { customAudiencesTOSs.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return customAudiencesTOSs; } // Sixth, check if it's pure JsonObject customAudiencesTOSs.clear(); customAudiencesTOSs.add(loadJSON(json, context)); return customAudiencesTOSs; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.CustomConversion.java
License:Open Source License
public static APINodeList<CustomConversion> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<CustomConversion> customConversions = new APINodeList<CustomConversion>(request, json); JsonArray arr;//www . j a v 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++) { customConversions.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return customConversions; } 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; customConversions.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++) { customConversions.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()) { customConversions.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { customConversions.add(loadJSON(obj.toString(), context)); } } return customConversions; } 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()) { customConversions.add(loadJSON(entry.getValue().toString(), context)); } return customConversions; } 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)) { customConversions.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return customConversions; } // Sixth, check if it's pure JsonObject customConversions.clear(); customConversions.add(loadJSON(json, context)); return customConversions; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.CustomConversionStatsResult.java
License:Open Source License
public static APINodeList<CustomConversionStatsResult> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<CustomConversionStatsResult> customConversionStatsResults = new APINodeList<CustomConversionStatsResult>( request, json);//from w ww . j a va2 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++) { customConversionStatsResults.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return customConversionStatsResults; } 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; customConversionStatsResults.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++) { customConversionStatsResults .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()) { customConversionStatsResults .add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { customConversionStatsResults.add(loadJSON(obj.toString(), context)); } } return customConversionStatsResults; } 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()) { customConversionStatsResults.add(loadJSON(entry.getValue().toString(), context)); } return customConversionStatsResults; } 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)) { customConversionStatsResults.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return customConversionStatsResults; } // Sixth, check if it's pure JsonObject customConversionStatsResults.clear(); customConversionStatsResults.add(loadJSON(json, context)); return customConversionStatsResults; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.DayPart.java
License:Open Source License
public static APINodeList<DayPart> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<DayPart> dayParts = new APINodeList<DayPart>(request, json); JsonArray arr;/*w w w . j a va2s . 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++) { dayParts.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return dayParts; } 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; dayParts.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++) { dayParts.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()) { dayParts.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { dayParts.add(loadJSON(obj.toString(), context)); } } return dayParts; } 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()) { dayParts.add(loadJSON(entry.getValue().toString(), context)); } return dayParts; } 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)) { dayParts.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return dayParts; } // Sixth, check if it's pure JsonObject dayParts.clear(); dayParts.add(loadJSON(json, context)); return dayParts; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.DeliveryCheck.java
License:Open Source License
public static APINodeList<DeliveryCheck> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<DeliveryCheck> deliveryChecks = new APINodeList<DeliveryCheck>(request, json); JsonArray arr;/* w w w. j a v a 2 s .co 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++) { deliveryChecks.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return deliveryChecks; } 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; deliveryChecks.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++) { deliveryChecks.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()) { deliveryChecks.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { deliveryChecks.add(loadJSON(obj.toString(), context)); } } return deliveryChecks; } 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()) { deliveryChecks.add(loadJSON(entry.getValue().toString(), context)); } return deliveryChecks; } 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)) { deliveryChecks.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return deliveryChecks; } // Sixth, check if it's pure JsonObject deliveryChecks.clear(); deliveryChecks.add(loadJSON(json, context)); return deliveryChecks; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.DeliveryCheckExtraInfo.java
License:Open Source License
public static APINodeList<DeliveryCheckExtraInfo> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<DeliveryCheckExtraInfo> deliveryCheckExtraInfos = new APINodeList<DeliveryCheckExtraInfo>( request, json);//from w w w . j a v a 2 s . com 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++) { deliveryCheckExtraInfos.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return deliveryCheckExtraInfos; } 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; deliveryCheckExtraInfos.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++) { deliveryCheckExtraInfos.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()) { deliveryCheckExtraInfos.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { deliveryCheckExtraInfos.add(loadJSON(obj.toString(), context)); } } return deliveryCheckExtraInfos; } 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()) { deliveryCheckExtraInfos.add(loadJSON(entry.getValue().toString(), context)); } return deliveryCheckExtraInfos; } 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)) { deliveryCheckExtraInfos.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return deliveryCheckExtraInfos; } // Sixth, check if it's pure JsonObject deliveryCheckExtraInfos.clear(); deliveryCheckExtraInfos.add(loadJSON(json, context)); return deliveryCheckExtraInfos; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.DeliveryEstimate.java
License:Open Source License
public static APINodeList<DeliveryEstimate> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<DeliveryEstimate> deliveryEstimates = new APINodeList<DeliveryEstimate>(request, json); JsonArray arr;/*from ww w .java 2s. c om*/ 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++) { deliveryEstimates.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return deliveryEstimates; } 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; deliveryEstimates.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++) { deliveryEstimates.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()) { deliveryEstimates.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { deliveryEstimates.add(loadJSON(obj.toString(), context)); } } return deliveryEstimates; } 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()) { deliveryEstimates.add(loadJSON(entry.getValue().toString(), context)); } return deliveryEstimates; } 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)) { deliveryEstimates.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return deliveryEstimates; } // Sixth, check if it's pure JsonObject deliveryEstimates.clear(); deliveryEstimates.add(loadJSON(json, context)); return deliveryEstimates; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.Domain.java
License:Open Source License
public static APINodeList<Domain> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<Domain> domains = new APINodeList<Domain>(request, json); JsonArray arr;//from w w w .ja v a2 s . c om 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++) { domains.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return domains; } 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; domains.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++) { domains.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()) { domains.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { domains.add(loadJSON(obj.toString(), context)); } } return domains; } 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()) { domains.add(loadJSON(entry.getValue().toString(), context)); } return domains; } 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)) { domains.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return domains; } // Sixth, check if it's pure JsonObject domains.clear(); domains.add(loadJSON(json, context)); return domains; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.Event.java
License:Open Source License
public static APINodeList<Event> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<Event> events = new APINodeList<Event>(request, json); JsonArray arr;/*ww w . jav a 2s . co 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++) { events.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return events; } 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; events.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++) { events.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()) { events.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { events.add(loadJSON(obj.toString(), context)); } } return events; } 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()) { events.add(loadJSON(entry.getValue().toString(), context)); } return events; } 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)) { events.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return events; } // Sixth, check if it's pure JsonObject events.clear(); events.add(loadJSON(json, context)); return events; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.EventSourceGroup.java
License:Open Source License
public static APINodeList<EventSourceGroup> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<EventSourceGroup> eventSourceGroups = new APINodeList<EventSourceGroup>(request, json); JsonArray arr;/* ww w .ja v 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++) { eventSourceGroups.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return eventSourceGroups; } 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; eventSourceGroups.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++) { eventSourceGroups.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()) { eventSourceGroups.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { eventSourceGroups.add(loadJSON(obj.toString(), context)); } } return eventSourceGroups; } 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()) { eventSourceGroups.add(loadJSON(entry.getValue().toString(), context)); } return eventSourceGroups; } 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)) { eventSourceGroups.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return eventSourceGroups; } // Sixth, check if it's pure JsonObject eventSourceGroups.clear(); eventSourceGroups.add(loadJSON(json, context)); return eventSourceGroups; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }