List of usage examples for com.google.gson JsonObject toString
@Override
public String toString()
From source file:com.facebook.ads.sdk.ProductGroup.java
License:Open Source License
public static APINodeList<ProductGroup> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<ProductGroup> productGroups = new APINodeList<ProductGroup>(request, json); JsonArray arr;//from w ww .jav 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++) { productGroups.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return productGroups; } 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; productGroups.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++) { productGroups.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()) { productGroups.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { productGroups.add(loadJSON(obj.toString(), context)); } } return productGroups; } 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()) { productGroups.add(loadJSON(entry.getValue().toString(), context)); } return productGroups; } 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)) { productGroups.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return productGroups; } // Sixth, check if it's pure JsonObject productGroups.clear(); productGroups.add(loadJSON(json, context)); return productGroups; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.ProductItem.java
License:Open Source License
public static APINodeList<ProductItem> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<ProductItem> productItems = new APINodeList<ProductItem>(request, json); JsonArray arr;//from w w w . ja va 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++) { productItems.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return productItems; } 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; productItems.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++) { productItems.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()) { productItems.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { productItems.add(loadJSON(obj.toString(), context)); } } return productItems; } 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()) { productItems.add(loadJSON(entry.getValue().toString(), context)); } return productItems; } 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)) { productItems.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return productItems; } // Sixth, check if it's pure JsonObject productItems.clear(); productItems.add(loadJSON(json, context)); return productItems; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.ProductItemCommerceInsights.java
License:Open Source License
public static APINodeList<ProductItemCommerceInsights> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<ProductItemCommerceInsights> productItemCommerceInsightss = new APINodeList<ProductItemCommerceInsights>( request, json);//from ww w .java 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++) { productItemCommerceInsightss.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return productItemCommerceInsightss; } 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; productItemCommerceInsightss.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++) { productItemCommerceInsightss .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()) { productItemCommerceInsightss .add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { productItemCommerceInsightss.add(loadJSON(obj.toString(), context)); } } return productItemCommerceInsightss; } 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()) { productItemCommerceInsightss.add(loadJSON(entry.getValue().toString(), context)); } return productItemCommerceInsightss; } 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)) { productItemCommerceInsightss.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return productItemCommerceInsightss; } // Sixth, check if it's pure JsonObject productItemCommerceInsightss.clear(); productItemCommerceInsightss.add(loadJSON(json, context)); return productItemCommerceInsightss; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.ProductSet.java
License:Open Source License
public static APINodeList<ProductSet> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<ProductSet> productSets = new APINodeList<ProductSet>(request, json); JsonArray arr;//from w w w . ja 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++) { productSets.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return productSets; } 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; productSets.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++) { productSets.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()) { productSets.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { productSets.add(loadJSON(obj.toString(), context)); } } return productSets; } 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()) { productSets.add(loadJSON(entry.getValue().toString(), context)); } return productSets; } 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)) { productSets.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return productSets; } // Sixth, check if it's pure JsonObject productSets.clear(); productSets.add(loadJSON(json, context)); return productSets; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.ProductVariant.java
License:Open Source License
public static APINodeList<ProductVariant> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<ProductVariant> productVariants = new APINodeList<ProductVariant>(request, json); JsonArray arr;/*from ww w. j a va 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++) { productVariants.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return productVariants; } 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; productVariants.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++) { productVariants.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()) { productVariants.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { productVariants.add(loadJSON(obj.toString(), context)); } } return productVariants; } 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()) { productVariants.add(loadJSON(entry.getValue().toString(), context)); } return productVariants; } 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)) { productVariants.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return productVariants; } // Sixth, check if it's pure JsonObject productVariants.clear(); productVariants.add(loadJSON(json, context)); return productVariants; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.ProfilePictureSource.java
License:Open Source License
public static APINodeList<ProfilePictureSource> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<ProfilePictureSource> profilePictureSources = new APINodeList<ProfilePictureSource>(request, json);/*from w ww.ja va 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++) { profilePictureSources.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return profilePictureSources; } 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; profilePictureSources.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++) { profilePictureSources.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()) { profilePictureSources.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { profilePictureSources.add(loadJSON(obj.toString(), context)); } } return profilePictureSources; } 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()) { profilePictureSources.add(loadJSON(entry.getValue().toString(), context)); } return profilePictureSources; } 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)) { profilePictureSources.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return profilePictureSources; } // Sixth, check if it's pure JsonObject profilePictureSources.clear(); profilePictureSources.add(loadJSON(json, context)); return profilePictureSources; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.RateCard.java
License:Open Source License
public static APINodeList<RateCard> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<RateCard> rateCards = new APINodeList<RateCard>(request, json); JsonArray arr;/*from w w w.jav a 2 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++) { rateCards.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return rateCards; } 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; rateCards.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++) { rateCards.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()) { rateCards.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { rateCards.add(loadJSON(obj.toString(), context)); } } return rateCards; } 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()) { rateCards.add(loadJSON(entry.getValue().toString(), context)); } return rateCards; } 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)) { rateCards.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return rateCards; } // Sixth, check if it's pure JsonObject rateCards.clear(); rateCards.add(loadJSON(json, context)); return rateCards; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.ReachEstimate.java
License:Open Source License
public static APINodeList<ReachEstimate> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<ReachEstimate> reachEstimates = new APINodeList<ReachEstimate>(request, json); JsonArray arr;/*from w w w . ja v a2 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++) { reachEstimates.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return reachEstimates; } 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; reachEstimates.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++) { reachEstimates.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()) { reachEstimates.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { reachEstimates.add(loadJSON(obj.toString(), context)); } } return reachEstimates; } 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()) { reachEstimates.add(loadJSON(entry.getValue().toString(), context)); } return reachEstimates; } 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)) { reachEstimates.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return reachEstimates; } // Sixth, check if it's pure JsonObject reachEstimates.clear(); reachEstimates.add(loadJSON(json, context)); return reachEstimates; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.ReachFrequencyPrediction.java
License:Open Source License
public static APINodeList<ReachFrequencyPrediction> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<ReachFrequencyPrediction> reachFrequencyPredictions = new APINodeList<ReachFrequencyPrediction>( request, json);//from ww w. j a va 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++) { reachFrequencyPredictions.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return reachFrequencyPredictions; } 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; reachFrequencyPredictions.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++) { reachFrequencyPredictions .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()) { reachFrequencyPredictions.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { reachFrequencyPredictions.add(loadJSON(obj.toString(), context)); } } return reachFrequencyPredictions; } 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()) { reachFrequencyPredictions.add(loadJSON(entry.getValue().toString(), context)); } return reachFrequencyPredictions; } 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)) { reachFrequencyPredictions.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return reachFrequencyPredictions; } // Sixth, check if it's pure JsonObject reachFrequencyPredictions.clear(); reachFrequencyPredictions.add(loadJSON(json, context)); return reachFrequencyPredictions; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.ReachFrequencySpec.java
License:Open Source License
public static APINodeList<ReachFrequencySpec> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<ReachFrequencySpec> reachFrequencySpecs = new APINodeList<ReachFrequencySpec>(request, json); JsonArray arr;/*from w w w . ja v a 2 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++) { reachFrequencySpecs.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return reachFrequencySpecs; } 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; reachFrequencySpecs.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++) { reachFrequencySpecs.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()) { reachFrequencySpecs.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { reachFrequencySpecs.add(loadJSON(obj.toString(), context)); } } return reachFrequencySpecs; } 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()) { reachFrequencySpecs.add(loadJSON(entry.getValue().toString(), context)); } return reachFrequencySpecs; } 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)) { reachFrequencySpecs.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return reachFrequencySpecs; } // Sixth, check if it's pure JsonObject reachFrequencySpecs.clear(); reachFrequencySpecs.add(loadJSON(json, context)); return reachFrequencySpecs; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }