Example usage for com.google.gson JsonObject getAsJsonObject

List of usage examples for com.google.gson JsonObject getAsJsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonObject getAsJsonObject.

Prototype

public JsonObject getAsJsonObject(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonObject.

Usage

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

License:Open Source License

public static APINodeList<WebAppLink> parseResponse(String json, APIContext context, APIRequest request)
        throws MalformedResponseException {
    APINodeList<WebAppLink> webAppLinks = new APINodeList<WebAppLink>(request, json);
    JsonArray arr;/*from   ww  w  .  j a v a  2  s . c  o  m*/
    JsonObject obj;
    JsonParser parser = new JsonParser();
    Exception exception = null;
    try {
        JsonElement result = parser.parse(json);
        if (result.isJsonArray()) {
            // First, check if it's a pure JSON Array
            arr = result.getAsJsonArray();
            for (int i = 0; i < arr.size(); i++) {
                webAppLinks.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return webAppLinks;
        } 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;
                    webAppLinks.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++) {
                        webAppLinks.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()) {
                                webAppLinks.add(loadJSON(entry.getValue().toString(), context));
                            }
                            break;
                        }
                    }
                    if (!isRedownload) {
                        webAppLinks.add(loadJSON(obj.toString(), context));
                    }
                }
                return webAppLinks;
            } 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()) {
                    webAppLinks.add(loadJSON(entry.getValue().toString(), context));
                }
                return webAppLinks;
            } 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)) {
                        webAppLinks.add(loadJSON(value.toString(), context));
                    } else {
                        isIdIndexedArray = false;
                        break;
                    }
                }
                if (isIdIndexedArray) {
                    return webAppLinks;
                }

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

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

License:Open Source License

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

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

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

License:Open Source License

public static APINodeList<WindowsPhoneAppLink> parseResponse(String json, APIContext context,
        APIRequest request) throws MalformedResponseException {
    APINodeList<WindowsPhoneAppLink> windowsPhoneAppLinks = new APINodeList<WindowsPhoneAppLink>(request, json);
    JsonArray arr;//from  ww  w.  j a  v  a 2 s  . co  m
    JsonObject obj;
    JsonParser parser = new JsonParser();
    Exception exception = null;
    try {
        JsonElement result = parser.parse(json);
        if (result.isJsonArray()) {
            // First, check if it's a pure JSON Array
            arr = result.getAsJsonArray();
            for (int i = 0; i < arr.size(); i++) {
                windowsPhoneAppLinks.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return windowsPhoneAppLinks;
        } 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;
                    windowsPhoneAppLinks.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++) {
                        windowsPhoneAppLinks.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()) {
                                windowsPhoneAppLinks.add(loadJSON(entry.getValue().toString(), context));
                            }
                            break;
                        }
                    }
                    if (!isRedownload) {
                        windowsPhoneAppLinks.add(loadJSON(obj.toString(), context));
                    }
                }
                return windowsPhoneAppLinks;
            } 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()) {
                    windowsPhoneAppLinks.add(loadJSON(entry.getValue().toString(), context));
                }
                return windowsPhoneAppLinks;
            } 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)) {
                        windowsPhoneAppLinks.add(loadJSON(value.toString(), context));
                    } else {
                        isIdIndexedArray = false;
                        break;
                    }
                }
                if (isIdIndexedArray) {
                    return windowsPhoneAppLinks;
                }

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

From source file:com.fb.marveltest.Cache.java

private Character createCharacter(final JsonObject jobject) {
    String id = jobject.get("id").toString();
    String name = jobject.get("name").toString().replace("\"", "");
    String description = jobject.get("description").toString().replace("\"", "");

    JsonObject t = jobject.getAsJsonObject("thumbnail");
    String thumbnailPath = t.get("path").toString().replace("\"", "");
    String thumbnailExtension = t.get("extension").toString().replace("\"", "");

    Thumbnail thumbnail = new Thumbnail(thumbnailPath, thumbnailExtension);
    return new Character(Long.valueOf(id), name, description, thumbnail);
}

From source file:com.fb.marveltest.CharacterListController.java

/**
 * Creates a new instance of CharacterListController
 *//*from w  ww.  j a  v a 2s .c o  m*/
public CharacterListController() {
    final String json = Cache.getInstance().getResource().queryParam("ts", "1").queryParam("limit", "100")
            .queryParam("offset", String.valueOf(offset))
            .queryParam("apikey", "7a4c44e4becce8ad14abb4f92544dc45")
            .queryParam("hash", "2553dc60bbf63062a13e64194ae026f5").accept(MediaType.APPLICATION_JSON)
            .get(String.class);

    JsonElement jelement = new JsonParser().parse(json);
    JsonObject jobject = jelement.getAsJsonObject();
    jobject = jobject.getAsJsonObject("data");
    JsonArray jarray = jobject.getAsJsonArray("results");

    for (JsonElement obj : jarray) {
        names.add(obj.getAsJsonObject().get("name").toString().replace("\"", ""));
    }
}

From source file:com.fb.marveltest.CharacterListController.java

public Character getCharacter() {
    final Cache cache = Cache.getInstance();
    character = cache.getCharacter(selectedName);
    if (character == null) {
        final String json = cache.getResource().queryParam("ts", "1").queryParam("name", selectedName)
                .queryParam("apikey", "7a4c44e4becce8ad14abb4f92544dc45")
                .queryParam("hash", "2553dc60bbf63062a13e64194ae026f5").accept(MediaType.APPLICATION_JSON)
                .get(String.class);
        JsonElement jelement = new JsonParser().parse(json);
        JsonObject jobject = jelement.getAsJsonObject();
        jobject = jobject.getAsJsonObject("data");
        JsonArray jarray = jobject.getAsJsonArray("results");
        jobject = jarray.get(0).getAsJsonObject();
        character = cache.addCharacter(jobject);
    }//from   w  w  w . j  a v  a2s.  c  om
    return character;
}

From source file:com.flipkart.android.proteus.parser.custom.ViewGroupParser.java

License:Apache License

private void handleDataDrivenChildren(LayoutBuilder builder, ProteusView parent, ProteusViewManager viewManager,
        JsonObject children, JsonObject data, Styles styles, int dataIndex) {

    String dataPath = children.get(ProteusConstants.DATA).getAsString().substring(1);
    viewManager.setDataPathForChildren(dataPath);

    Result result = Utils.readJson(dataPath, data, dataIndex);
    JsonElement element = result.isSuccess() ? result.element : null;

    JsonObject childLayout = children.getAsJsonObject(ProteusConstants.LAYOUT);

    viewManager.setChildLayout(childLayout);

    if (null == element || element.isJsonNull()) {
        return;/*w ww.  j a va 2 s. c om*/
    }

    int length = element.getAsJsonArray().size();

    ProteusView child;
    for (int index = 0; index < length; index++) {
        child = builder.build((ViewGroup) parent, childLayout, data, index, styles);
        if (child != null) {
            this.addView(parent, child);
        }
    }
}

From source file:com.fooock.shodan.model.host.FacetReportDeserializer.java

License:Open Source License

private FacetReport parseFacetReport(JsonObject jsonObject, JsonElement totalElement) {
    final int total = totalElement.getAsInt();
    JsonObject facetsElement = jsonObject.getAsJsonObject("facets");
    if (facetsElement == null || facetsElement.isJsonNull()) {
        return new FacetReport(total, Collections.emptyList());
    }/*from w  ww  . ja  va2s  .  c  om*/
    return new FacetReport(total, getFacets(facetsElement));
}

From source file:com.football.site.getdata.ScoreWebService.java

private static LeagueTeams GetLeagueTeamsFromJson(String data) {
    LeagueTeams ligTeams = null;/*from   ww w  .  jav a2 s .c o m*/
    try {
        JsonParser parser = new JsonParser();
        Gson gson = new Gson();
        ligTeams = gson.fromJson(data, LeagueTeams.class);
        ligTeams.set_links(new LinksLeagueTeams());
        JsonObject obj = parser.parse(data).getAsJsonObject();
        JsonObject linkler = obj.getAsJsonObject("_links");
        ligTeams.get_links().setSelf(linkler.getAsJsonObject("self").get("href").getAsString());
        ligTeams.get_links().setSoccerseason(linkler.getAsJsonObject("soccerseason").get("href").getAsString());
    } catch (Exception e) {
        HelperUtil.AddErrorLog(logger, e);
    }
    return ligTeams;
}

From source file:com.football.site.getdata.ScoreWebService.java

public static Fixtures GetFixture(String url) {
    Fixtures result = null;//from  w  w w.  j  a  v  a  2 s.c  om
    try {
        JsonParser parser = new JsonParser();
        String response = GetHttpClientResponse(url);
        Gson gson = new Gson();
        result = gson.fromJson(response, Fixtures.class);
        LinksFixtures lf = new LinksFixtures();
        JsonObject obj = parser.parse(response).getAsJsonObject();
        JsonObject linkler = obj.getAsJsonObject("_links");
        lf.setSelf(linkler.getAsJsonObject("self").get("href").getAsString());
        if (linkler.has("soccerseason")) {
            lf.setSoccerseason(linkler.getAsJsonObject("soccerseason").get("href").getAsString());
        }
        if (linkler.has("team")) {
            lf.setTeam(linkler.getAsJsonObject("team").get("href").getAsString());
        }
        result.setLinks(lf);
    } catch (Exception e) {
        HelperUtil.AddErrorLog(logger, e);
    }
    return result;
}