List of usage examples for com.google.gson JsonArray size
public int size()
From source file:com.facebook.ads.sdk.AdStudy.java
License:Open Source License
public static APINodeList<AdStudy> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdStudy> adStudys = new APINodeList<AdStudy>(request, json); JsonArray arr; JsonObject obj;/*w w w . j av a 2 s . co m*/ 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++) { adStudys.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return adStudys; } 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; adStudys.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++) { adStudys.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()) { adStudys.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { adStudys.add(loadJSON(obj.toString(), context)); } } return adStudys; } 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()) { adStudys.add(loadJSON(entry.getValue().toString(), context)); } return adStudys; } 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)) { adStudys.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return adStudys; } // Sixth, check if it's pure JsonObject adStudys.clear(); adStudys.add(loadJSON(json, context)); return adStudys; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.AdStudyCell.java
License:Open Source License
public static APINodeList<AdStudyCell> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdStudyCell> adStudyCells = new APINodeList<AdStudyCell>(request, json); JsonArray arr; JsonObject obj;//from www .j a v a2s . c o m 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++) { adStudyCells.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return adStudyCells; } 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; adStudyCells.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++) { adStudyCells.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()) { adStudyCells.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { adStudyCells.add(loadJSON(obj.toString(), context)); } } return adStudyCells; } 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()) { adStudyCells.add(loadJSON(entry.getValue().toString(), context)); } return adStudyCells; } 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)) { adStudyCells.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return adStudyCells; } // Sixth, check if it's pure JsonObject adStudyCells.clear(); adStudyCells.add(loadJSON(json, context)); return adStudyCells; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.AdStudyObjective.java
License:Open Source License
public static APINodeList<AdStudyObjective> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdStudyObjective> adStudyObjectives = new APINodeList<AdStudyObjective>(request, json); JsonArray arr; JsonObject obj;//from w w w. j a va 2 s. c o m 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++) { adStudyObjectives.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return adStudyObjectives; } 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; adStudyObjectives.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++) { adStudyObjectives.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()) { adStudyObjectives.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { adStudyObjectives.add(loadJSON(obj.toString(), context)); } } return adStudyObjectives; } 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()) { adStudyObjectives.add(loadJSON(entry.getValue().toString(), context)); } return adStudyObjectives; } 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)) { adStudyObjectives.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return adStudyObjectives; } // Sixth, check if it's pure JsonObject adStudyObjectives.clear(); adStudyObjectives.add(loadJSON(json, context)); return adStudyObjectives; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.AgencyClientDeclaration.java
License:Open Source License
public static APINodeList<AgencyClientDeclaration> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AgencyClientDeclaration> agencyClientDeclarations = new APINodeList<AgencyClientDeclaration>( request, json);/* ww w. ja v a2 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++) { agencyClientDeclarations.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return agencyClientDeclarations; } 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; agencyClientDeclarations.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++) { agencyClientDeclarations .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()) { agencyClientDeclarations.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { agencyClientDeclarations.add(loadJSON(obj.toString(), context)); } } return agencyClientDeclarations; } 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()) { agencyClientDeclarations.add(loadJSON(entry.getValue().toString(), context)); } return agencyClientDeclarations; } 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)) { agencyClientDeclarations.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return agencyClientDeclarations; } // Sixth, check if it's pure JsonObject agencyClientDeclarations.clear(); agencyClientDeclarations.add(loadJSON(json, context)); return agencyClientDeclarations; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.AndroidAppLink.java
License:Open Source License
public static APINodeList<AndroidAppLink> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AndroidAppLink> androidAppLinks = new APINodeList<AndroidAppLink>(request, json); JsonArray arr; JsonObject obj;/*from w w w. j av a 2s .c o m*/ 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++) { androidAppLinks.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return androidAppLinks; } 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; androidAppLinks.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++) { androidAppLinks.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()) { androidAppLinks.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { androidAppLinks.add(loadJSON(obj.toString(), context)); } } return androidAppLinks; } 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()) { androidAppLinks.add(loadJSON(entry.getValue().toString(), context)); } return androidAppLinks; } 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)) { androidAppLinks.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return androidAppLinks; } // Sixth, check if it's pure JsonObject androidAppLinks.clear(); androidAppLinks.add(loadJSON(json, context)); return androidAppLinks; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.APINode.java
License:Open Source License
public static APINodeList parseResponse(String json, APIContext context, APIRequest<APINode> request) throws MalformedResponseException { APINodeList<APINode> nodes = new APINodeList<APINode>(request, json); JsonArray arr; JsonObject obj;/*from w w w .j a v a 2 s . c om*/ 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++) { nodes.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return nodes; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); nodes.setPaging(paging.get("before").getAsString(), paging.get("after").getAsString()); } 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++) { nodes.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(); nodes.add(loadJSON(obj.toString(), context)); } return nodes; } 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()) { nodes.add(loadJSON(entry.getValue().toString(), context)); } return nodes; } 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)) { nodes.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return nodes; } // Sixth, check if it's pure JsonObject nodes.clear(); nodes.add(loadJSON(json, context)); return nodes; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.AppLinks.java
License:Open Source License
public static APINodeList<AppLinks> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AppLinks> appLinkss = new APINodeList<AppLinks>(request, json); JsonArray arr; JsonObject obj;/*from w w w .jav a 2 s. c o m*/ 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++) { appLinkss.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return appLinkss; } 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; appLinkss.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++) { appLinkss.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()) { appLinkss.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { appLinkss.add(loadJSON(obj.toString(), context)); } } return appLinkss; } 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()) { appLinkss.add(loadJSON(entry.getValue().toString(), context)); } return appLinkss; } 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)) { appLinkss.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return appLinkss; } // Sixth, check if it's pure JsonObject appLinkss.clear(); appLinkss.add(loadJSON(json, context)); return appLinkss; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.BatchRequest.java
License:Open Source License
public List<APIResponse> execute() throws APIException { try {//from ww w.j a v a 2 s. c o m List<APIResponse> responses = new ArrayList<APIResponse>(); String batchResponse = executeInternal(); context.log(batchResponse); JsonArray jsonArray = new JsonParser().parse(batchResponse).getAsJsonArray(); if (jsonArray.size() != requests.size()) { throw new APIException.MalformedResponseException( "Batch request size is " + requests.size() + ", but response size is " + jsonArray.size()); } for (int i = 0; i < jsonArray.size(); i++) { if (jsonArray.get(i).isJsonNull()) { responses.add(null); continue; } JsonObject response = jsonArray.get(i).getAsJsonObject(); if (response == null || response.get("body") == null || response.get("body").isJsonNull()) { responses.add(null); continue; } if (response.get("code").getAsInt() == HttpURLConnection.HTTP_OK) { String body = response.get("body").getAsString(); APIRequest request = requests.get(i).request; responses.add(request.parseResponse(body)); } else { responses.add(new APIException.FailedRequestException(response.toString())); } } return responses; } catch (IOException e) { throw new APIException.FailedRequestException(e); } }
From source file:com.facebook.ads.sdk.BroadTargetingCategories.java
License:Open Source License
public static APINodeList<BroadTargetingCategories> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<BroadTargetingCategories> broadTargetingCategoriess = new APINodeList<BroadTargetingCategories>( 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++) { broadTargetingCategoriess.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return broadTargetingCategoriess; } 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; broadTargetingCategoriess.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++) { broadTargetingCategoriess .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()) { broadTargetingCategoriess.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { broadTargetingCategoriess.add(loadJSON(obj.toString(), context)); } } return broadTargetingCategoriess; } 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()) { broadTargetingCategoriess.add(loadJSON(entry.getValue().toString(), context)); } return broadTargetingCategoriess; } 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)) { broadTargetingCategoriess.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return broadTargetingCategoriess; } // Sixth, check if it's pure JsonObject broadTargetingCategoriess.clear(); broadTargetingCategoriess.add(loadJSON(json, context)); return broadTargetingCategoriess; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.Business.java
License:Open Source License
public static APINodeList<Business> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<Business> businesss = new APINodeList<Business>(request, json); JsonArray arr; JsonObject obj;/*from w w w. j a va 2 s. c o m*/ 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++) { businesss.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return businesss; } 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; businesss.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++) { businesss.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()) { businesss.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { businesss.add(loadJSON(obj.toString(), context)); } } return businesss; } 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()) { businesss.add(loadJSON(entry.getValue().toString(), context)); } return businesss; } 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)) { businesss.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return businesss; } // Sixth, check if it's pure JsonObject businesss.clear(); businesss.add(loadJSON(json, context)); return businesss; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }