public String parse(String jsonLine) { JsonElement jelement = new JsonParser().parse(jsonLine); JsonObject jobject = jelement.getAsJsonObject(); jobject = jobject.getAsJsonObject("data"); JsonArray jarray = jobject.getAsJsonArray("translations"); jobject = jarray.get(0).getAsJsonObject(); String result = jobject.get("translatedText").toString(); return result; }
@Override public BlockVector3 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonArray jsonArray = json.getAsJsonArray(); if (jsonArray.size() != 3) { throw new JsonParseException("Expected array of 3 length for BlockVector3"); } double x = jsonArray.get(0).getAsInt(); double y = jsonArray.get(1).getAsInt(); double z = jsonArray.get(2).getAsInt(); return BlockVector3.at(x, y, z); } }
public static JsonArray removeElementFromJsonArray(JsonArray inputJsonArray, String key) { JsonArray outputJsonArray = new JsonArray(); for (int i = 0; i < inputJsonArray.size(); i += 1) { JsonObject jsonObject = inputJsonArray.get(i).getAsJsonObject(); outputJsonArray.add(removeElementFromJsonObject(jsonObject, key)); } return outputJsonArray; }
if (rollups != null) for (int j = 0; j < rollups.size(); j++) JsonObject rollupObject = rollups.get(j).getAsJsonObject(); Rollup rollup = m_gson.fromJson(rollupObject, Rollup.class);
private JsonElement getSchemaData(String methodName) { return testData.get(methodName).getAsJsonArray().get(0); } }
private void processSingleRequestResponse(CloseableHttpResponse response) throws IOException, UnexpectedResponseException { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode < 400) { return; } String entityStr = EntityUtils.toString(response.getEntity()); if (statusCode == 400 && Operation.INSERT_ONLY_NOT_EXIST.equals(operation) && entityStr != null) { //Ignore if it's duplicate entry error code JsonArray jsonArray = new JsonParser().parse(entityStr).getAsJsonArray(); JsonObject jsonObject = jsonArray.get(0).getAsJsonObject(); if (isDuplicate(jsonObject, statusCode)) { return; } } throw new RuntimeException("Failed due to " + entityStr + " (Detail: " + ToStringBuilder.reflectionToString(response, ToStringStyle.SHORT_PREFIX_STYLE) + " )"); }
private class VkAudioAlbumsResponseDeserializer implements JsonDeserializer<VkAudioAlbumsResponse> { @Override public VkAudioAlbumsResponse deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JsonArray jArray = (JsonArray) json; int firstInteger = jArray.get(0); //ignore the 1st int VkAudioAlbumsResponse vkAudioAlbumsResponse = new VkAudioAlbumsResponse(); for (int i=1; i<jArray.size(); i++) { JsonObject jObject = (JsonObject) jArray.get(i); //assuming you have the suitable constructor... VkAudioAlbum album = new VkAudioAlbum(jObject.get("owner_id").getAsInt(), jObject.get("album_id").getAsInt(), jObject.get("title").getAsString()); vkAudioAlbumsResponse.getResponse().add(album); } return vkAudioAlbumsResponse; } }
@Override public String[] getCallbackIP() throws WxErrorException { String url = "https://api.weixin.qq.com/cgi-bin/getcallbackip"; String responseContent = get(url, null); JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); JsonArray ipList = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray(); String[] ipArray = new String[ipList.size()]; for (int i = 0; i < ipList.size(); i++) { ipArray[i] = ipList.get(i).getAsString(); } return ipArray; }
private Map<String, PortBinding> mapPortBindings(JsonArray ports) { int length = ports.size(); Map<String, PortBinding> portBindings = new HashMap<>(length); for (int i = 0; i < length; i++) { JsonObject object = ports.get(i).getAsJsonObject(); portBindings.put(createPortKey(object), createPortBinding(object)); } return portBindings; } }
return; for (int i = 0; i < cameras.size(); i++) { JsonObject camObj = cameras.get(i).getAsJsonObject(); String type = getAsString(camObj, "type"); assertNotNull(type, "No type defined ofr camera"); addToCache("cameras", i, cam, cameras.size());
@Override public Quat4f deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); if (array.size() == 4) { return new Quat4f(array.get(0).getAsFloat(), array.get(1).getAsFloat(), array.get(2).getAsFloat(), array.get(3).getAsFloat()); } } return null; } }
private JsonElement getExpectedSchema(String methodName) { return testData.get(methodName).getAsJsonArray().get(1); }
@Test public void testAllCases() throws DataConversionException { for (Map.Entry<String, JsonElement> keyset : testJsonData.entrySet()) { JsonArray testData = keyset.getValue().getAsJsonArray(); JsonObject json = testData.get(0).getAsJsonObject(); JsonArray schema = testData.get(1).getAsJsonArray(); JsonObject expected = testData.get(2).getAsJsonObject(); JsonObject result = null; try { result = parseJsonObject(json, schema); } catch (Exception e) { e.printStackTrace(); assertEquals("Test case failed : " + keyset.getKey(), "No exception", e.getMessage()); } assertEquals("Test case failed : " + keyset.getKey(), expected, result); } }
@Override public String[] getCallbackIp() throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/getcallbackip"; String responseContent = get(url, null); JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); JsonArray jsonArray = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray(); String[] ips = new String[jsonArray.size()]; for(int i = 0; i < jsonArray.size(); i++) { ips[i] = jsonArray.get(i).getAsString(); } return ips; }
public JsonSchema getFieldSchemaAt(int i) { if (i >= this.jsonArray.size()) { return new JsonSchema(this.json); } return new JsonSchema(this.jsonArray.get(i).getAsJsonObject()); }
@Override public Vector4f deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); if (array.size() == 4) { return new Vector4f(array.get(0).getAsFloat(), array.get(1).getAsFloat(), array.get(2).getAsFloat(), array.get(3).getAsFloat()); } else if (array.size() == 3) { return new Vector4f(array.get(0).getAsFloat(), array.get(1).getAsFloat(), array.get(2).getAsFloat(), 1); } } return null; } }
@Override protected Void doInBackground(Void... params) { try { JsonObject jsonObject = HttpUtil.getResposeJsonObject(BMA.Billboard.billSongList(BILLBOARD_KING, 0, 3)); JsonArray array = jsonObject.get("song_list").getAsJsonArray(); for (int i = 0; i < array.size(); i++) { BillboardInfo billboardInfo = new BillboardInfo(); billboardInfo.title = array.get(i).getAsJsonObject().get("title").toString(); billboardInfo.author = array.get(i).getAsJsonObject().get("author").toString(); billboardInfo.id = array.get(i).getAsJsonObject().get("artist_id").toString(); items.add(billboardInfo); } } catch (NullPointerException e) { e.printStackTrace(); } return null; }
@Override public List<Network> listNetworks() throws DockerAccessException { String url = urlBuilder.listNetworks(); try { String response = delegate.get(url, HTTP_OK); JsonArray array = JsonFactory.newJsonArray(response); List<Network> networks = new ArrayList<>(array.size()); for (int i = 0; i < array.size(); i++) { networks.add(new NetworksListElement(array.get(i).getAsJsonObject())); } return networks; } catch (IOException e) { throw new DockerAccessException(e.getMessage()); } }
@Override public Vector3 deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonArray jsonArray = json.getAsJsonArray(); if (jsonArray.size() != 3) { throw new JsonParseException("Expected array of 3 length for Vector3"); } double x = jsonArray.get(0).getAsDouble(); double y = jsonArray.get(1).getAsDouble(); double z = jsonArray.get(2).getAsDouble(); return Vector3.at(x, y, z); } }
private String[][] parseResultArray(JsonArray esResponse) { List<Map.Entry<String, JsonElement>> fieldsInFirstResponseRow = Lists.newArrayList(esResponse.get(0).getAsJsonObject().entrySet()); String[][] result = new String[esResponse.size() + 1][fieldsInFirstResponseRow.size()]; for(int i = 0; i < fieldsInFirstResponseRow.size(); i++) { result[0][i] = fieldsInFirstResponseRow.get(i).getKey(); } int rowNum = 1; for(JsonElement row: esResponse) { JsonObject currentObj = row.getAsJsonObject(); for (int colId = 0; colId < fieldsInFirstResponseRow.size(); colId++) { result[rowNum][colId] = currentObj.get(result[0][colId]).getAsString(); } rowNum++; } return result; } }