本文整理了Java中com.google.gson.JsonElement.getAsString()
方法的一些代码示例,展示了JsonElement.getAsString()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonElement.getAsString()
方法的具体详情如下:
包路径:com.google.gson.JsonElement
类名称:JsonElement
方法名:getAsString
[英]convenience method to get this element as a string value.
[中]将此元素作为字符串值获取的方便方法。
代码示例来源:origin: Vedenin/useful-java-links
/**
* 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!"*/
}
代码示例来源:origin: apache/incubator-gobblin
/**
* 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;
}
代码示例来源:origin: MovingBlocks/Terasology
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() + "'");
}
}
}
}
代码示例来源:origin: gocd/gocd
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();
}
代码示例来源:origin: chanjarster/weixin-java-tools
@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();
}
代码示例来源:origin: searchbox-io/Jest
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());
});
代码示例来源:origin: apache/incubator-gobblin
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;
}
代码示例来源:origin: MovingBlocks/Terasology
@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());
代码示例来源:origin: apache/zeppelin
@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);
}
代码示例来源:origin: SonarSource/sonarqube
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()));
}
代码示例来源:origin: gocd/gocd
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();
}
代码示例来源:origin: apache/incubator-gobblin
/**
* 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;
}
}
代码示例来源:origin: chanjarster/weixin-java-tools
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();
}
代码示例来源:origin: apache/incubator-gobblin
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 {
代码示例来源:origin: MovingBlocks/Terasology
@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());
代码示例来源:origin: SonarSource/sonarqube
@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;
}
}
代码示例来源:origin: gocd/gocd
@Test
public void shouldAppendTypeFieldWhenSerializingAntTask()
{
CRTask value = antTask;
JsonObject jsonObject = (JsonObject)gson.toJsonTree(value);
assertThat(jsonObject.get("type").getAsString(), is("ant"));
}
@Test
代码示例来源:origin: gocd/gocd
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;
}
代码示例来源:origin: chanjarster/weixin-java-tools
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));
}
代码示例来源:origin: aa112901/remusic
try {
JsonObject jsonObject = HttpUtil.getResposeJsonObject(BMA.GeDan.geDanInfo(playlsitId + ""));
JsonArray pArray = jsonObject.get("content").getAsJsonArray();
playlistDetail = jsonObject.get("desc").getAsString();
mHandler.post(showInfo);
内容来源于网络,如有侵权,请联系作者删除!