Example usage for com.google.gson JsonElement isJsonObject

List of usage examples for com.google.gson JsonElement isJsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonObject.

Prototype

public boolean isJsonObject() 

Source Link

Document

provides check for verifying if this element is a Json object or not.

Usage

From source file:com.epishie.tabs.gson.ThingDeserializer.java

License:Apache License

@Override
public Thing deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("Expected json object");
    }//from   w w  w.  jav a2s.  c om
    JsonObject jsonObject = json.getAsJsonObject();
    if (!jsonObject.has(KIND)) {
        throw new JsonParseException("Expected to have \"kind\" field");
    }
    JsonElement kindElement = jsonObject.get(KIND);
    if (!kindElement.isJsonPrimitive()) {
        throw new JsonParseException("Expected to have a primitive \"kind\" field");
    }
    if (!jsonObject.has(DATA)) {
        throw new JsonParseException("Expected to have a\"data\" field");
    }
    String kind = kindElement.getAsString();
    String id = null;
    String name = null;
    if (jsonObject.has(ID)) {
        JsonElement idElement = jsonObject.get(ID);
        id = idElement.getAsString();
    }
    if (jsonObject.has(NAME)) {
        JsonElement nameElement = jsonObject.get(NAME);
        name = nameElement.getAsString();
    }
    Thing thing = null;
    switch (kind) {
    case Link.KIND:
        Link link = context.deserialize(jsonObject.get(DATA), Link.class);
        thing = new Thing<>(id, name, link);
        break;
    case Listing.KIND:
        Listing listing = context.deserialize(jsonObject.get(DATA), Listing.class);
        // noinspection unchecked
        thing = new Thing(id, name, listing);
        break;
    case Subreddit.KIND:
        Subreddit subreddit = context.deserialize(jsonObject.get(DATA), Subreddit.class);
        // noinspection unchecked
        thing = new Thing(id, name, subreddit);
        break;
    case Comment.KIND:
        Comment comment = context.deserialize(jsonObject.get(DATA), Comment.class);
        // noinspection unchecked
        thing = new Thing(id, name, comment);
        break;
    }
    return thing;
}

From source file:com.ericsson.eiffel.remrem.semantics.schemas.SchemaFile.java

License:Apache License

/**
 * This method is used to iterate over the items array because items is an
 * array of jsonObject/* w  w  w.ja v a  2  s .c  o m*/
 * 
 * @param elementName
 *            - Json element name as an input parameter to this method
 * @param jsonValue
 *            - Json element value as an input parameter to this method
 * @param jsonObject
 *            - JsonObject which is having property 'Items' is an input to
 *            this method
 * @param previousObjectName
 *            - name of the previousObject to this items object is an input
 *            parameter to this method
 */
private void addingItemsProperties(String elementName, JsonElement jsonValue, JsonObject jsonObject,
        String previousObjectName) {
    if (jsonValue.isJsonObject()) {
        JsonObject newJsonObj = new JsonObject();
        if (elementName.equals(EiffelConstants.ITEMS)) {
            addAttributesToJsonSchema(jsonValue.getAsJsonObject(), modifyClassName(previousObjectName),
                    newJsonObj);
        } else {
            addAttributesToJsonSchema(jsonValue.getAsJsonObject(), elementName, newJsonObj);
        }
        jsonObject.add(elementName, newJsonObj);
    }
}

From source file:com.example.android.xyztouristattractions.ui.AttractionListFragment.java

License:Open Source License

@Override
public void success(JsonElement string, Response response) {
    JsonObject mJsonObject = null;//  ww  w.  j  av a 2s.  c  om
    List<Attraction> attractionsList = new ArrayList<Attraction>();
    if (string.isJsonObject()) {
        //            Toast.makeText(getActivity(), "OnSuccess--Object" + string.toString(), Toast.LENGTH_LONG).show();
        mJsonObject = string.getAsJsonObject();
        JsonElement mElement = mJsonObject.get("attractions");
        if (mElement.isJsonArray()) {
            JsonArray mArray = mElement.getAsJsonArray();
            HashMap<String, List<Attraction>> ATTRACTIONS = new HashMap<String, List<Attraction>>();

            String city = "";
            for (JsonElement element : mArray) {
                JsonObject attractions = null;
                if (element.isJsonObject()) {
                    attractions = element.getAsJsonObject();
                    JsonObject imageURl = attractions.get("imageUrl").getAsJsonObject();
                    JsonObject secImageURl = attractions.get("secondaryImageUrl").getAsJsonObject();
                    JsonObject location = attractions.get("location").getAsJsonObject();
                    city = attractions.get("city").toString();
                    //                        Toast.makeText(getActivity(), "OnSuccess--" + attractions.get("name") + imageURl.get("uriString")
                    //                                + attractions.get("city") + attractions.get("longDescription")
                    //                                + attractions.get("description"), Toast.LENGTH_LONG).show();
                    //                        String name, String description, String longDescription, Uri imageUrl,
                    //                                Uri secondaryImageUrl, LatLng location, String city
                    attractionsList.add(new Attraction(attractions.get("name").toString(),
                            attractions.get("description").toString(),
                            attractions.get("longDescription").toString(),
                            Uri.parse(imageURl.get("uriString").toString()),
                            Uri.parse(secImageURl.get("uriString").toString()),
                            new LatLng(Float.parseFloat(location.get("latitude").toString()),
                                    Float.parseFloat(location.get("longitude").toString())),
                            attractions.get("city").toString()));
                    Log.d(AttractionListFragment.class.getSimpleName(), imageURl.get("uriString").toString());
                    Log.d(AttractionListFragment.class.getSimpleName(),
                            secImageURl.get("uriString").toString());

                } else
                    Toast.makeText(getActivity(), "OnSuccess--Object--Array--Not Object", Toast.LENGTH_LONG)
                            .show();
            }
            ATTRACTIONS.put(city, attractionsList);

        } else
            Toast.makeText(getActivity(), "OnSuccess--Object--Not Array", Toast.LENGTH_LONG).show();
    } else
        Toast.makeText(getActivity(), "OnSuccess--Not Object", Toast.LENGTH_LONG).show();

    //        DataModel model = new Gson().fromJson(string.toString(), DataModel.class);
    //        List<Attraction> attractions = model.getAttractions();
    mAdapter = new AttractionAdapter(getActivity(), attractionsList);
    recyclerView.setAdapter(mAdapter);
}

From source file:com.facebook.ads.sdk.AdAccount.java

License:Open Source License

public static APINodeList<AdAccount> parseResponse(String json, APIContext context, APIRequest request)
        throws MalformedResponseException {
    APINodeList<AdAccount> adAccounts = new APINodeList<AdAccount>(request, json);
    JsonArray arr;//w  w  w  .ja v  a  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++) {
                adAccounts.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return adAccounts;
        } 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;
                    adAccounts.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++) {
                        adAccounts.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()) {
                                adAccounts.add(loadJSON(entry.getValue().toString(), context));
                            }
                            break;
                        }
                    }
                    if (!isRedownload) {
                        adAccounts.add(loadJSON(obj.toString(), context));
                    }
                }
                return adAccounts;
            } 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()) {
                    adAccounts.add(loadJSON(entry.getValue().toString(), context));
                }
                return adAccounts;
            } 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)) {
                        adAccounts.add(loadJSON(value.toString(), context));
                    } else {
                        isIdIndexedArray = false;
                        break;
                    }
                }
                if (isIdIndexedArray) {
                    return adAccounts;
                }

                // Sixth, check if it's pure JsonObject
                adAccounts.clear();
                adAccounts.add(loadJSON(json, context));
                return adAccounts;
            }
        }
    } catch (Exception e) {
        exception = e;
    }
    throw new MalformedResponseException("Invalid response string: " + json, exception);
}

From source file:com.facebook.ads.sdk.AdAccountDeliveryEstimate.java

License:Open Source License

public static APINodeList<AdAccountDeliveryEstimate> parseResponse(String json, APIContext context,
        APIRequest request) throws MalformedResponseException {
    APINodeList<AdAccountDeliveryEstimate> adAccountDeliveryEstimates = new APINodeList<AdAccountDeliveryEstimate>(
            request, json);//from w  ww.  j a va2 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++) {
                adAccountDeliveryEstimates.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return adAccountDeliveryEstimates;
        } 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;
                    adAccountDeliveryEstimates.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++) {
                        adAccountDeliveryEstimates
                                .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()) {
                                adAccountDeliveryEstimates.add(loadJSON(entry.getValue().toString(), context));
                            }
                            break;
                        }
                    }
                    if (!isRedownload) {
                        adAccountDeliveryEstimates.add(loadJSON(obj.toString(), context));
                    }
                }
                return adAccountDeliveryEstimates;
            } 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()) {
                    adAccountDeliveryEstimates.add(loadJSON(entry.getValue().toString(), context));
                }
                return adAccountDeliveryEstimates;
            } 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)) {
                        adAccountDeliveryEstimates.add(loadJSON(value.toString(), context));
                    } else {
                        isIdIndexedArray = false;
                        break;
                    }
                }
                if (isIdIndexedArray) {
                    return adAccountDeliveryEstimates;
                }

                // Sixth, check if it's pure JsonObject
                adAccountDeliveryEstimates.clear();
                adAccountDeliveryEstimates.add(loadJSON(json, context));
                return adAccountDeliveryEstimates;
            }
        }
    } catch (Exception e) {
        exception = e;
    }
    throw new MalformedResponseException("Invalid response string: " + json, exception);
}

From source file:com.facebook.ads.sdk.AdAccountGroup.java

License:Open Source License

public static APINodeList<AdAccountGroup> parseResponse(String json, APIContext context, APIRequest request)
        throws MalformedResponseException {
    APINodeList<AdAccountGroup> adAccountGroups = new APINodeList<AdAccountGroup>(request, json);
    JsonArray arr;/* ww w  . j ava2s  . 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++) {
                adAccountGroups.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return adAccountGroups;
        } 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;
                    adAccountGroups.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++) {
                        adAccountGroups.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()) {
                                adAccountGroups.add(loadJSON(entry.getValue().toString(), context));
                            }
                            break;
                        }
                    }
                    if (!isRedownload) {
                        adAccountGroups.add(loadJSON(obj.toString(), context));
                    }
                }
                return adAccountGroups;
            } 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()) {
                    adAccountGroups.add(loadJSON(entry.getValue().toString(), context));
                }
                return adAccountGroups;
            } 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)) {
                        adAccountGroups.add(loadJSON(value.toString(), context));
                    } else {
                        isIdIndexedArray = false;
                        break;
                    }
                }
                if (isIdIndexedArray) {
                    return adAccountGroups;
                }

                // Sixth, check if it's pure JsonObject
                adAccountGroups.clear();
                adAccountGroups.add(loadJSON(json, context));
                return adAccountGroups;
            }
        }
    } catch (Exception e) {
        exception = e;
    }
    throw new MalformedResponseException("Invalid response string: " + json, exception);
}

From source file:com.facebook.ads.sdk.AdAccountGroupResult.java

License:Open Source License

public static APINodeList<AdAccountGroupResult> parseResponse(String json, APIContext context,
        APIRequest request) throws MalformedResponseException {
    APINodeList<AdAccountGroupResult> adAccountGroupResults = new APINodeList<AdAccountGroupResult>(request,
            json);// w w 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++) {
                adAccountGroupResults.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return adAccountGroupResults;
        } 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;
                    adAccountGroupResults.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++) {
                        adAccountGroupResults.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()) {
                                adAccountGroupResults.add(loadJSON(entry.getValue().toString(), context));
                            }
                            break;
                        }
                    }
                    if (!isRedownload) {
                        adAccountGroupResults.add(loadJSON(obj.toString(), context));
                    }
                }
                return adAccountGroupResults;
            } 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()) {
                    adAccountGroupResults.add(loadJSON(entry.getValue().toString(), context));
                }
                return adAccountGroupResults;
            } 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)) {
                        adAccountGroupResults.add(loadJSON(value.toString(), context));
                    } else {
                        isIdIndexedArray = false;
                        break;
                    }
                }
                if (isIdIndexedArray) {
                    return adAccountGroupResults;
                }

                // Sixth, check if it's pure JsonObject
                adAccountGroupResults.clear();
                adAccountGroupResults.add(loadJSON(json, context));
                return adAccountGroupResults;
            }
        }
    } catch (Exception e) {
        exception = e;
    }
    throw new MalformedResponseException("Invalid response string: " + json, exception);
}

From source file:com.facebook.ads.sdk.AdAccountRoas.java

License:Open Source License

public static APINodeList<AdAccountRoas> parseResponse(String json, APIContext context, APIRequest request)
        throws MalformedResponseException {
    APINodeList<AdAccountRoas> adAccountRoass = new APINodeList<AdAccountRoas>(request, json);
    JsonArray arr;/*from   w  w w.ja  v a 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++) {
                adAccountRoass.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return adAccountRoass;
        } 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;
                    adAccountRoass.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++) {
                        adAccountRoass.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()) {
                                adAccountRoass.add(loadJSON(entry.getValue().toString(), context));
                            }
                            break;
                        }
                    }
                    if (!isRedownload) {
                        adAccountRoass.add(loadJSON(obj.toString(), context));
                    }
                }
                return adAccountRoass;
            } 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()) {
                    adAccountRoass.add(loadJSON(entry.getValue().toString(), context));
                }
                return adAccountRoass;
            } 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)) {
                        adAccountRoass.add(loadJSON(value.toString(), context));
                    } else {
                        isIdIndexedArray = false;
                        break;
                    }
                }
                if (isIdIndexedArray) {
                    return adAccountRoass;
                }

                // Sixth, check if it's pure JsonObject
                adAccountRoass.clear();
                adAccountRoass.add(loadJSON(json, context));
                return adAccountRoass;
            }
        }
    } catch (Exception e) {
        exception = e;
    }
    throw new MalformedResponseException("Invalid response string: " + json, exception);
}

From source file:com.facebook.ads.sdk.AdAccountRoasCohorts.java

License:Open Source License

public static APINodeList<AdAccountRoasCohorts> parseResponse(String json, APIContext context,
        APIRequest request) {//w ww .  ja  v a2s .  c  om
    APINodeList<AdAccountRoasCohorts> adAccountRoasCohortss = new APINodeList<AdAccountRoasCohorts>(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++) {
                adAccountRoasCohortss.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return adAccountRoasCohortss;
        } else if (result.isJsonObject()) {
            obj = result.getAsJsonObject();
            if (obj.has("data")) {
                try {
                    JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject();
                    adAccountRoasCohortss.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++) {
                        adAccountRoasCohortss.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();
                    adAccountRoasCohortss.add(loadJSON(obj.toString(), context));
                }
                return adAccountRoasCohortss;
            } 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()) {
                    adAccountRoasCohortss.add(loadJSON(entry.getValue().toString(), context));
                }
                return adAccountRoasCohortss;
            } else {
                // Fifth, check if it's pure JsonObject
                adAccountRoasCohortss.add(loadJSON(json, context));
                return adAccountRoasCohortss;
            }
        }
    } catch (Exception e) {
    }
    return null;
}

From source file:com.facebook.ads.sdk.AdAccountTargetingBrowse.java

License:Open Source License

public static APINodeList<AdAccountTargetingBrowse> parseResponse(String json, APIContext context,
        APIRequest request) {/*w  ww . ja v a2 s .  c om*/
    APINodeList<AdAccountTargetingBrowse> adAccountTargetingBrowses = new APINodeList<AdAccountTargetingBrowse>(
            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++) {
                adAccountTargetingBrowses.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return adAccountTargetingBrowses;
        } else if (result.isJsonObject()) {
            obj = result.getAsJsonObject();
            if (obj.has("data")) {
                try {
                    JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject();
                    adAccountTargetingBrowses.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++) {
                        adAccountTargetingBrowses
                                .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();
                    adAccountTargetingBrowses.add(loadJSON(obj.toString(), context));
                }
                return adAccountTargetingBrowses;
            } 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()) {
                    adAccountTargetingBrowses.add(loadJSON(entry.getValue().toString(), context));
                }
                return adAccountTargetingBrowses;
            } else {
                // Fifth, check if it's pure JsonObject
                adAccountTargetingBrowses.add(loadJSON(json, context));
                return adAccountTargetingBrowses;
            }
        }
    } catch (Exception e) {
    }
    return null;
}