/** * 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!"*/ }
/** * Get optional property from a {@link JsonObject} for a {@link String} key. * If key does'nt exists returns {@link #DEFAULT_VALUE_FOR_OPTIONAL_PROPERTY}. * @param jsonObject * @param key * @return */ public static String getOptionalProperty(JsonObject jsonObject, String key) { return jsonObject.has(key) ? jsonObject.get(key).getAsString() : DEFAULT_VALUE_FOR_OPTIONAL_PROPERTY; }
private void readSoundList(JsonObject element, String field, List<StaticSound> sounds) throws IOException { if (element.has(field) && element.get(field).isJsonArray()) { sounds.clear(); for (JsonElement item : element.getAsJsonArray(field)) { Optional<StaticSound> sound = assetManager.getAsset(item.getAsString(), StaticSound.class); if (sound.isPresent()) { sounds.add(sound.get()); } else { throw new IOException("Unable to resolve sound '" + item.getAsString() + "'"); } } } }
public Optional<String> optString(String property) { if (hasJsonObject(property)) { try { return Optional.ofNullable(jsonObject.get(property).getAsString()); } catch (Exception e) { throw haltBecausePropertyIsNotAJsonString(property, jsonObject); } } return Optional.empty(); }
@Override public String tagCreate(String tagName) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/create"; JsonObject o = new JsonObject(); o.addProperty("tagname", tagName); String responseContent = post(url, o.toString()); JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); return tmpJsonElement.getAsJsonObject().get("tagid").getAsString(); }
JsonObject hitObject = hitElement.getAsJsonObject(); JsonObject source = hitObject.getAsJsonObject(sourceKey); String index = hitObject.get("_index").getAsString(); String type = hitObject.get("_type").getAsString(); String id = hitObject.get("_id").getAsString(); if (hitObject.has("_score") && !hitObject.get("_score").isJsonNull()) { score = hitObject.get("_score").getAsDouble(); String routing = null; if (hitObject.has("_parent") && !hitObject.get("_parent").isJsonNull()) { parent = hitObject.get("_parent").getAsString(); if (hitObject.has("_routing") && !hitObject.get("_routing").isJsonNull()) { routing = hitObject.get("_routing").getAsString(); if (hitObject.has("matched_queries") && !hitObject.get("matched_queries").isJsonNull()) { JsonArray rawMatchedQueries = hitObject.get("matched_queries").getAsJsonArray(); rawMatchedQueries.forEach(matchedQuery -> { matchedQueries.add(matchedQuery.getAsString()); });
public EnumConverter(JsonSchema schema, String namespace) { super(schema); JsonObject dataType = schema.getDataType(); for (JsonElement elem : dataType.get(ENUM_SYMBOLS_KEY).getAsJsonArray()) { this.enumSet.add(elem.getAsString()); } String enumName = schema.getName(); this.enumName = enumName.isEmpty() ? null : enumName; this.namespace = namespace; }
@Override public EntityData.Prefab deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { EntityData.Prefab.Builder prefab = EntityData.Prefab.newBuilder(); JsonObject jsonObject = json.getAsJsonObject(); case "name": if (entry.getValue().isJsonPrimitive()) { prefab.setName(entry.getValue().getAsString()); prefab.setParentName(entry.getValue().getAsString()); prefab.addRemovedComponent(entry.getValue().getAsString()); } else if (entry.getValue().isJsonArray()) { for (JsonElement element : entry.getValue().getAsJsonArray()) { prefab.addRemovedComponent(element.getAsString());
@Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); String label = (labelJsonElement == null ? null : labelJsonElement.getAsString()); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); }
public static QPMeasureData fromJson(String json) { return new QPMeasureData(StreamSupport.stream(new JsonParser().parse(json).getAsJsonArray().spliterator(), false) .map(jsonElement -> { JsonObject jsonProfile = jsonElement.getAsJsonObject(); return new QualityProfile( jsonProfile.get("key").getAsString(), jsonProfile.get("name").getAsString(), jsonProfile.get("language").getAsString(), UtcDateUtils.parseDateTime(jsonProfile.get("rulesUpdatedAt").getAsString())); }).collect(Collectors.toList())); }
public Optional<CaseInsensitiveString> optCaseInsensitiveString(String property) { if (hasJsonObject(property)) { try { return Optional.of(new CaseInsensitiveString(jsonObject.get(property).getAsString())); } catch (Exception e) { throw haltBecausePropertyIsNotAJsonString(property, jsonObject); } } return Optional.empty(); }
/** * Get optional property from a {@link JsonObject} for a {@link String} key. * If key does'nt exists returns {@link #DEFAULT_VALUE_FOR_OPTIONAL_PROPERTY}. * @param jsonObject * @param key * @return */ private String getOptionalProperty(JsonObject jsonObject, String key) { return jsonObject.has(key) ? jsonObject.get(key).getAsString() : DEFAULT_VALUE_FOR_OPTIONAL_PROPERTY; } }
public String shortUrl(String long_url) throws WxErrorException { String url = "https://api.weixin.qq.com/cgi-bin/shorturl"; JsonObject o = new JsonObject(); o.addProperty("action", "long2short"); o.addProperty("long_url", long_url); String responseContent = execute(new SimplePostRequestExecutor(), url, o.toString()); JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); return tmpJsonElement.getAsJsonObject().get("short_url").getAsString(); }
throws JsonParseException { List<JobExecutionPlan> jobExecutionPlans = new ArrayList<>(); JsonArray jsonArray = json.getAsJsonArray(); JsonObject jobSpecJson = (JsonObject) serializedJobExecutionPlan.get(SerializationConstants.JOB_SPEC_KEY); JsonObject specExecutorJson = (JsonObject) serializedJobExecutionPlan.get(SerializationConstants.SPEC_EXECUTOR_KEY); ExecutionStatus executionStatus = ExecutionStatus.valueOf(serializedJobExecutionPlan. get(SerializationConstants.EXECUTION_STATUS_KEY).getAsString()); String uri = jobSpecJson.get(SerializationConstants.JOB_SPEC_URI_KEY).getAsString(); String version = jobSpecJson.get(SerializationConstants.JOB_SPEC_VERSION_KEY).getAsString(); String description = jobSpecJson.get(SerializationConstants.JOB_SPEC_DESCRIPTION_KEY).getAsString(); String templateURI = jobSpecJson.get(SerializationConstants.JOB_SPEC_TEMPLATE_URI_KEY).getAsString(); String config = jobSpecJson.get(SerializationConstants.JOB_SPEC_CONFIG_KEY).getAsString(); Config jobConfig = ConfigFactory.parseString(config); JobSpec jobSpec; Config specExecutorConfig = ConfigFactory.parseString(specExecutorJson.get(SerializationConstants.SPEC_EXECUTOR_CONFIG_KEY).getAsString()); String className = specExecutorJson.get(SerializationConstants.SPEC_EXECUTOR_CLASS_KEY).getAsString(); SpecExecutor specExecutor; try {
@Override public EntityData.Entity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { EntityData.Entity.Builder entity = EntityData.Entity.newBuilder(); JsonObject jsonObject = json.getAsJsonObject(); case "parentprefab": if (entry.getValue().isJsonPrimitive()) { entity.setParentPrefab(entry.getValue().getAsString()); case "removedcomponent": if (entry.getValue().isJsonArray()) { for (JsonElement element : entry.getValue().getAsJsonArray()) { entity.addRemovedComponent(element.getAsString());
@CheckForNull private static String tryParseAsJsonError(String responseContent) { try { JsonParser parser = new JsonParser(); JsonObject obj = parser.parse(responseContent).getAsJsonObject(); JsonArray errors = obj.getAsJsonArray("errors"); List<String> errorMessages = new ArrayList<>(); for (JsonElement e : errors) { errorMessages.add(e.getAsJsonObject().get("msg").getAsString()); } return Joiner.on(", ").join(errorMessages); } catch (Exception e) { return null; } }
@Test public void shouldAppendTypeFieldWhenSerializingAntTask() { CRTask value = antTask; JsonObject jsonObject = (JsonObject)gson.toJsonTree(value); assertThat(jsonObject.get("type").getAsString(), is("ant")); } @Test
public static EnvironmentVariableConfig fromJSON(JsonObject jsonReader) { String name = jsonReader.get("name").getAsString(); String value = jsonReader.get("value").getAsString(); boolean secure = jsonReader.has("secure") && jsonReader.get("secure").getAsBoolean(); EnvironmentVariableConfig environmentConfig = new EnvironmentVariableConfig(new GoCipher(), name, value, secure); return environmentConfig; }
public String templateSend(WxMpTemplateMessage templateMessage) throws WxErrorException { String url = "https://api.weixin.qq.com/cgi-bin/message/template/send"; String responseContent = execute(new SimplePostRequestExecutor(), url, templateMessage.toJson()); JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent))); final JsonObject jsonObject = tmpJsonElement.getAsJsonObject(); if (jsonObject.get("errcode").getAsInt() == 0) return jsonObject.get("msgid").getAsString(); throw new WxErrorException(WxError.fromJson(responseContent)); }
try { JsonObject jsonObject = HttpUtil.getResposeJsonObject(BMA.GeDan.geDanInfo(playlsitId + "")); JsonArray pArray = jsonObject.get("content").getAsJsonArray(); playlistDetail = jsonObject.get("desc").getAsString(); mHandler.post(showInfo);