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 String getImage() { // ID: json.getString("Image"); return json.getAsJsonObject(CONFIG).get(IMAGE).getAsString(); }
public int getBulkRetryCount() { return (jsonObject != null && jsonObject.has("retries") && jsonObject.getAsJsonObject("retries").has("bulk")) ? jsonObject.getAsJsonObject("retries").get("bulk").getAsInt() : 0; }
public WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem wxMaterialNewsBatchGetNewsItem = new WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem(); JsonObject json = jsonElement.getAsJsonObject(); if (json.get("media_id") != null && !json.get("media_id").isJsonNull()) { wxMaterialNewsBatchGetNewsItem.setMediaId(GsonHelper.getAsString(json.get("media_id"))); } if (json.get("update_time") != null && !json.get("update_time").isJsonNull()) { wxMaterialNewsBatchGetNewsItem.setUpdateTime(new Date(1000 * GsonHelper.getAsLong(json.get("update_time")))); } if (json.get("content") != null && !json.get("content").isJsonNull()) { JsonObject newsItem = json.getAsJsonObject("content"); wxMaterialNewsBatchGetNewsItem.setContent(WxMpGsonBuilder.create().fromJson(newsItem, WxMpMaterialNews.class)); } return wxMaterialNewsBatchGetNewsItem; } }
public String[] getArguments() { if (!json.has(PROCESS_CONFIG)) { return null; } JsonObject processConfig = json.getAsJsonObject(PROCESS_CONFIG); if (!processConfig.has(ARGUMENTS)) { return null; } JsonArray arguments = processConfig.getAsJsonArray(ARGUMENTS); String[] result = new String[arguments.size()]; for (int i = 0; i < arguments.size(); i++) { result[i] = arguments.get(i).getAsString(); } return result; } }
String myJson = "[{\"id\":4,\"quantity\":2,\"product\":{\"id\":2,\"productName\":\"Audi R8\",\"description\":\"The best of Audi\",\"cost\":1000000.0,\"rrp\":1500000.0,\"product_category\":[{\"id\":2,\"category\":{\"id\":1,\"categoryname\":\"Supercars\"}},{\"id\":3,\"category\":{\"id\":2,\"categoryname\":\"Sportscars\"}}]}}]"; JsonElement jsonElement = new JsonParser().parse(myJson); // This is because your json string is an array {} JsonArray myJsonAsArray = jsonElement.getAsJsonArray(); // Now inside the json array, you get an object, so get it first from '0' index JsonObject mainObject = myJsonAsArray.get(0).getAsJsonObject(); // Now you have a 'product' JsonObject inside the main object // You can directly get it by using String name JsonObject productObject = mainObject.getAsJsonObject("product"); // Once you have 'Product' object, you can get individual elements by following String productName = productObject.get("productName").getAsString(); // This will print out "Audi R8" System.out.println(productName); //You know how it works. Now get other individual elements of the product object by name
JsonParser parser = new JsonParser(); JsonObject object1 = parser.parse(json).getAsJsonObject().getAsJsonObject("object1"); int userId = object1.getAsJsonObject("user").getAsJsonPrimitive("id").getAsInt(); result.setUserId(userId); JsonArray list = object1.getAsJsonObject("example").getAsJsonArray("list"); for (int i = 0; i < list.size(); ++i) JsonObject entry = list.get(i).getAsJsonObject(); String name = entry.getAsJsonPrimitive("name").getAsString();
/** * @param aggName Name of the AvgAggregation * @return a new AvgAggregation object if aggName is found within sub-aggregations of current aggregation level or null if not found */ public AvgAggregation getAvgAggregation(String aggName) { return jsonRoot.has(aggName) ? new AvgAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; }
@Override public boolean isRunning() { JsonObject state = json.getAsJsonObject(STATE); return state.get(RUNNING).getAsBoolean(); }
/** * Example to readJson using TreeModel */ private static void readJson() throws IOException { JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse("{\"message\":\"Hi\",\"place\":{\"name\":\"World!\"}}"); JsonObject rootObject = jsonElement.getAsJsonObject(); String message = rootObject.get("message").getAsString(); // get property "message" JsonObject childObject = rootObject.getAsJsonObject("place"); // get place object String place = childObject.get("name").getAsString(); // get property "name" System.out.println(message + " " + place); // print "Hi World!"*/ }
public int getSearchRetryCount() { return (jsonObject != null && jsonObject.has("retries") && jsonObject.getAsJsonObject("retries").has("search")) ? jsonObject.getAsJsonObject("retries").get("search").getAsInt() : 0; }
private void throwDockerAccessException(JsonObject json) throws DockerAccessException { String msg = json.get("error").getAsString().trim(); String details = json.getAsJsonObject("errorDetail").get("message").getAsString().trim(); throw new DockerAccessException("%s %s", msg, (msg.equals(details) ? "" : "(" + details + ")")); }
@VisibleForTesting static RegistrationRequest updateSLCapabilities(RegistrationRequest registrationRequest, String url) { String currentName = Thread.currentThread().getName(); Thread.currentThread().setName("SauceLabs"); JsonElement slAccountInfo = getCommonProxyUtilities().readJSONFromUrl(url, SAUCE_LABS_USER_NAME, SAUCE_LABS_ACCESS_KEY); try { registrationRequest.getConfiguration().capabilities.clear(); String logMessage = String.format("Account max. concurrency fetched from %s", url); int sauceLabsAccountConcurrency; if (slAccountInfo == null) { logMessage = String.format("Account max. concurrency was NOT fetched from %s", url); sauceLabsAccountConcurrency = 1; } else { sauceLabsAccountConcurrency = slAccountInfo.getAsJsonObject().getAsJsonObject("concurrency_limit"). get("overall").getAsInt(); } LOGGER.info(logMessage); Thread.currentThread().setName(currentName); return addCapabilitiesToRegistrationRequest(registrationRequest, sauceLabsAccountConcurrency, SAUCE_LABS_PROXY_NAME); } catch (Exception e) { LOGGER.warn(e.toString(), e); getGa().trackException(e); } Thread.currentThread().setName(currentName); return addCapabilitiesToRegistrationRequest(registrationRequest, 1, SAUCE_LABS_PROXY_NAME); }
public static void main(String[] args) { String json = getJson(); System.out.println(json); parse(json); } public static void parse(String jsonLine) { JsonObject root = new JsonParser().parse(jsonLine).getAsJsonObject(); JsonArray terms = root.getAsJsonObject("facets").getAsJsonObject("pie") .getAsJsonArray("terms"); for (int i = 0; i < terms.size(); i++) { JsonObject term = terms.get(i).getAsJsonObject(); System.out.printf("%s - %s \n", term.get("term").toString(), term.get("count").toString()); } }
/** * @param aggName Name of the GeoBoundsAggregation * @return a new GeoBoundsAggregation object if aggName is found within sub-aggregations of current aggregation level or null if not found */ public GeoBoundsAggregation getGeoBoundsAggregation(String aggName) { return jsonRoot.has(aggName) ? new GeoBoundsAggregation(aggName, jsonRoot.getAsJsonObject(aggName)) : null; }
@Override public Integer getExitCode() { if (isRunning()) { return null; } JsonObject state = json.getAsJsonObject(STATE); return state.get(EXIT_CODE).getAsInt(); }
public boolean isHealthy() { final JsonObject state = json.getAsJsonObject(STATE); // always indicate healthy for docker hosts that do not support health checks. return !state.has(HEALTH) || HEALTH_STATUS_HEALTHY.equals(state.getAsJsonObject(HEALTH).get(STATUS).getAsString()); }
@Override public Map<String, PortBinding> getPortBindings() { if (json.has(NETWORK_SETTINGS) && !json.get(NETWORK_SETTINGS).isJsonNull()) { JsonObject networkSettings = json.getAsJsonObject(NETWORK_SETTINGS); if (networkSettings.has(PORTS) && !networkSettings.get(PORTS).isJsonNull()) { return createPortBindings(networkSettings.getAsJsonObject(PORTS)); } } return new HashMap<>(); }
ConfigurationKeys.ENABLE_DELIMITED_IDENTIFIER, ConfigurationKeys.DEFAULT_ENABLE_DELIMITED_IDENTIFIER); JsonObject defaultWatermark = this.getDefaultWatermark(); String derivedWatermarkColumnName = defaultWatermark.get("columnName").getAsString(); this.setSampleRecordCount(this.exractSampleRecordCountFromQuery(inputQuery)); inputQuery = this.removeSampleClauseFromQuery(inputQuery); Schema obj = this.getUpdatedSchemaObject(columnName, alias, targetColumnName); String jsonStr = gson.toJson(obj); JsonObject jsonObject = gson.fromJson(jsonStr, JsonObject.class).getAsJsonObject(); targetSchema.add(jsonObject); headerColumns.add(targetColumnName);
} else { String sourceKey = keys[keys.length - 1]; JsonElement obj = jsonObject.get(keys[0]); if (keys.length > 1) { for (int i = 1; i < keys.length - 1; i++) { obj = ((JsonObject) obj).get(keys[i]); JsonElement source = obj.getAsJsonObject().get(sourceKey); if (source != null) { sourceList.add(source); for (JsonElement element : obj.getAsJsonArray()) { if (element instanceof JsonObject) { JsonObject currentObj = element.getAsJsonObject(); JsonObject source = currentObj.getAsJsonObject(sourceKey); if (source != null) { JsonObject copy = (JsonObject) CloneUtils.deepClone(source); JsonElement copy = CloneUtils.deepClone(obj); if (addEsMetadataFields && copy.isJsonObject()) { JsonObject copyObject = copy.getAsJsonObject(); for (MetaField metaField : META_FIELDS) { JsonElement metaElement = jsonObject.get(metaField.esFieldName);