List of usage examples for com.google.gson JsonArray get
public JsonElement get(int i)
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; JsonObject obj;//w w w . java2 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++) { 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; JsonObject obj;//from w w w. ja va 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++) { 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; JsonObject obj;//from w w w. j a v 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++) { 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.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); }/* w w w . jav a 2 s .c o m*/ return character; }
From source file:com.flipkart.android.proteus.toolbox.ColorUtils.java
License:Apache License
private static ColorStateList inflateFromJson(Context context, JsonObject jsonObject) { ColorStateList result = null;/*from w w w . j ava2 s.c o m*/ JsonElement type = jsonObject.get("type"); if (null != type && type.isJsonPrimitive()) { String colorType = type.getAsString(); if (TextUtils.equals(colorType, "selector")) { JsonElement childrenElement = jsonObject.get("children"); if (null != childrenElement && childrenElement.isJsonArray()) { JsonArray children = childrenElement.getAsJsonArray(); int listAllocated = 20; int listSize = 0; int[] colorList = new int[listAllocated]; int[][] stateSpecList = new int[listAllocated][]; for (int idx = 0; idx < children.size(); idx++) { JsonElement itemObject = children.get(idx); if (!itemObject.isJsonObject()) { continue; } Set<Map.Entry<String, JsonElement>> entrySet = ((JsonObject) itemObject).entrySet(); if (entrySet.size() == 0) { continue; } int j = 0; Integer baseColor = null; float alphaMod = 1.0f; int[] stateSpec = new int[entrySet.size() - 1]; boolean ignoreItem = false; for (Map.Entry<String, JsonElement> entry : entrySet) { if (ignoreItem) { break; } if (!entry.getValue().isJsonPrimitive()) { continue; } Integer attributeId = getAttribute(entry.getKey()); if (null != attributeId) { switch (attributeId) { case android.R.attr.type: if (!TextUtils.equals("item", entry.getValue().getAsString())) { ignoreItem = true; } break; case android.R.attr.color: String colorRes = entry.getValue().getAsString(); if (!TextUtils.isEmpty(colorRes)) { baseColor = getColorFromAttributeValue(context, colorRes); } break; case android.R.attr.alpha: String alphaStr = entry.getValue().getAsString(); if (!TextUtils.isEmpty(alphaStr)) { alphaMod = Float.parseFloat(alphaStr); } break; default: stateSpec[j++] = entry.getValue().getAsBoolean() ? attributeId : -attributeId; break; } } } if (!ignoreItem) { stateSpec = StateSet.trimStateSet(stateSpec, j); if (null == baseColor) { throw new IllegalStateException("No Color Specified"); } if (listSize + 1 >= listAllocated) { listAllocated = idealIntArraySize(listSize + 1); int[] ncolor = new int[listAllocated]; System.arraycopy(colorList, 0, ncolor, 0, listSize); int[][] nstate = new int[listAllocated][]; System.arraycopy(stateSpecList, 0, nstate, 0, listSize); colorList = ncolor; stateSpecList = nstate; } final int color = modulateColorAlpha(baseColor, alphaMod); colorList[listSize] = color; stateSpecList[listSize] = stateSpec; listSize++; } } if (listSize > 0) { int[] colors = new int[listSize]; int[][] stateSpecs = new int[listSize][]; System.arraycopy(colorList, 0, colors, 0, listSize); System.arraycopy(stateSpecList, 0, stateSpecs, 0, listSize); result = new ColorStateList(stateSpecs, colors); } } } } return result; }
From source file:com.flipkart.android.proteus.toolbox.Utils.java
License:Apache License
public static Result readJson(String path, JsonObject data, int index) { // replace INDEX reference with index value if (ProteusConstants.INDEX.equals(path)) { path = path.replace(ProteusConstants.INDEX, String.valueOf(index)); return Result.success(new JsonPrimitive(path)); } else {/* ww w.j a v a2 s. c om*/ StringTokenizer tokenizer = new StringTokenizer(path, ProteusConstants.DATA_PATH_DELIMITERS); JsonElement elementToReturn = data; JsonElement tempElement; JsonArray tempArray; while (tokenizer.hasMoreTokens()) { String segment = tokenizer.nextToken(); if (elementToReturn == null) { return Result.NO_SUCH_DATA_PATH_EXCEPTION; } if (elementToReturn.isJsonNull()) { return Result.JSON_NULL_EXCEPTION; } if ("".equals(segment)) { continue; } if (elementToReturn.isJsonArray()) { tempArray = elementToReturn.getAsJsonArray(); if (ProteusConstants.INDEX.equals(segment)) { if (index < tempArray.size()) { elementToReturn = tempArray.get(index); } else { return Result.NO_SUCH_DATA_PATH_EXCEPTION; } } else if (ProteusConstants.ARRAY_DATA_LENGTH_REFERENCE.equals(segment)) { elementToReturn = new JsonPrimitive(tempArray.size()); } else if (ProteusConstants.ARRAY_DATA_LAST_INDEX_REFERENCE.equals(segment)) { if (tempArray.size() == 0) { return Result.NO_SUCH_DATA_PATH_EXCEPTION; } elementToReturn = tempArray.get(tempArray.size() - 1); } else { try { index = Integer.parseInt(segment); } catch (NumberFormatException e) { return Result.INVALID_DATA_PATH_EXCEPTION; } if (index < tempArray.size()) { elementToReturn = tempArray.get(index); } else { return Result.NO_SUCH_DATA_PATH_EXCEPTION; } } } else if (elementToReturn.isJsonObject()) { tempElement = elementToReturn.getAsJsonObject().get(segment); if (tempElement != null) { elementToReturn = tempElement; } else { return Result.NO_SUCH_DATA_PATH_EXCEPTION; } } else if (elementToReturn.isJsonPrimitive()) { return Result.INVALID_DATA_PATH_EXCEPTION; } else { return Result.NO_SUCH_DATA_PATH_EXCEPTION; } } if (elementToReturn.isJsonNull()) { return Result.JSON_NULL_EXCEPTION; } return Result.success(elementToReturn); } }
From source file:com.flipkart.android.proteus.toolbox.Utils.java
License:Apache License
public static JsonElement merge(JsonElement oldJson, JsonElement newJson, boolean useCopy, Gson gson) { JsonElement newDataElement;/*from w ww. j av a 2 s . c o m*/ JsonArray oldArray; JsonArray newArray; JsonElement oldArrayItem; JsonElement newArrayItem; JsonObject oldObject; if (oldJson == null || oldJson.isJsonNull()) { return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson; } if (newJson == null || newJson.isJsonNull()) { newJson = JsonNull.INSTANCE; return newJson; } if (newJson.isJsonPrimitive()) { JsonPrimitive value; if (!useCopy) { return newJson; } if (newJson.getAsJsonPrimitive().isBoolean()) { value = new JsonPrimitive(newJson.getAsBoolean()); } else if (newJson.getAsJsonPrimitive().isNumber()) { value = new JsonPrimitive(newJson.getAsNumber()); } else if (newJson.getAsJsonPrimitive().isString()) { value = new JsonPrimitive(newJson.getAsString()); } else { value = newJson.getAsJsonPrimitive(); } return value; } if (newJson.isJsonArray()) { if (!oldJson.isJsonArray()) { return useCopy ? gson.fromJson(newJson, JsonArray.class) : newJson; } else { oldArray = oldJson.getAsJsonArray(); newArray = newJson.getAsJsonArray(); if (oldArray.size() > newArray.size()) { while (oldArray.size() > newArray.size()) { oldArray.remove(oldArray.size() - 1); } } for (int index = 0; index < newArray.size(); index++) { if (index < oldArray.size()) { oldArrayItem = oldArray.get(index); newArrayItem = newArray.get(index); oldArray.set(index, merge(oldArrayItem, newArrayItem, useCopy, gson)); } else { oldArray.add(newArray.get(index)); } } } } else if (newJson.isJsonObject()) { if (!oldJson.isJsonObject()) { return useCopy ? gson.fromJson(newJson, JsonObject.class) : newJson; } else { oldObject = oldJson.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : newJson.getAsJsonObject().entrySet()) { newDataElement = merge(oldObject.get(entry.getKey()), entry.getValue(), useCopy, gson); oldObject.add(entry.getKey(), newDataElement); } } } else { return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson; } return oldJson; }
From source file:com.flipkart.android.proteus.toolbox.Utils.java
License:Apache License
public static String getStringFromArray(JsonArray array, String delimiter) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < array.size(); i++) { if (array.get(i).isJsonPrimitive()) { sb.append(array.get(i).getAsString()); } else {//from w w w.j av a 2s .co m sb.append(array.get(i).toString()); } if (i < array.size() - 1) { sb.append(delimiter).append(" "); } } return sb.toString(); }
From source file:com.foobnix.api.vkontakte.VkGsonResponse.java
License:Open Source License
public static <T> List<T> toModels(String json, Class<T> clazz, int startIndex) throws VkErrorException { List<T> result = new ArrayList<T>(); if (StringUtils.isEmpty(json) || json.trim().equalsIgnoreCase("{\"response\":{}}")) { LOG.d("Recive an empty json", json); return result; }/*ww w.j a v a 2 s . c om*/ if (json.trim().startsWith("{\"error\":")) { throw new VkErrorException(json); } Gson gson = new Gson(); JsonParser parser = new JsonParser(); JsonArray array = parser.parse(json).getAsJsonObject().getAsJsonArray("response"); for (int i = startIndex; i < array.size(); i++) { JsonElement next = array.get(i); T fromJson = gson.fromJson(next, clazz); result.add(fromJson); } return result; }
From source file:com.foobnix.vkontakte.VkGsonResponse.java
License:Open Source License
public static <T> List<T> toModels(String json, Class<T> clazz, int startIndex) throws VkErrorException { List<T> result = new ArrayList<T>(); if (json == null || json.trim().isEmpty() || json.trim().equalsIgnoreCase("{\"response\":{}}")) { log.debug("Recive an empty json" + json); return result; }// w w w . j a v a 2 s .c o m if (json.trim().startsWith("{\"error\":")) { throw new VkErrorException(json); } Gson gson = new Gson(); JsonParser parser = new JsonParser(); JsonArray array = parser.parse(json).getAsJsonObject().getAsJsonArray("response"); for (int i = startIndex; i < array.size(); i++) { JsonElement next = array.get(i); T fromJson = gson.fromJson(next, clazz); result.add(fromJson); } return result; }