本文整理了Java中com.google.gson.JsonElement.getAsJsonArray()
方法的一些代码示例,展示了JsonElement.getAsJsonArray()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonElement.getAsJsonArray()
方法的具体详情如下:
包路径:com.google.gson.JsonElement
类名称:JsonElement
方法名:getAsJsonArray
[英]convenience method to get this element as a JsonArray. If the element is of some other type, a IllegalStateException will result. Hence it is best to use this method after ensuring that this element is of the desired type by calling #isJsonArray()first.
[中]将此元素作为JsonArray获取的方便方法。如果该元素属于其他类型,则会导致IllegalStateException。因此,最好先调用#isJsonArray()以确保此元素为所需类型,然后再使用此方法。
代码示例来源:origin: chanjarster/weixin-java-tools
@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;
}
代码示例来源:origin: gocd/gocd
@Override
public Filters deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
final JsonObject j = json.getAsJsonObject();
final JsonElement filters = j.get(KEY_FILTERS);
if (null == filters) {
throw new JsonParseException("Missing filters array!");
}
final ArrayList<DashboardFilter> viewFilters = new ArrayList<>();
filters.getAsJsonArray().forEach((f) -> viewFilters.add(context.deserialize(f, DashboardFilter.class)));
return new Filters(viewFilters);
}
}
代码示例来源:origin: MovingBlocks/Terasology
@Override
public SetMultimap<String, V> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
SetMultimap<String, V> result = HashMultimap.create();
JsonObject obj = json.getAsJsonObject();
for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
if (entry.getValue().isJsonArray()) {
for (JsonElement item : entry.getValue().getAsJsonArray()) {
result.put(entry.getKey(), context.<V>deserialize(item, valueType));
}
} else {
result.put(entry.getKey(), context.<V>deserialize(entry.getValue(), valueType));
}
}
return result;
}
代码示例来源:origin: searchbox-io/Jest
protected <T, K> Hit<T, K> extractHit(Class<T> sourceType, Class<K> explanationType, JsonElement hitElement, String sourceKey, boolean addEsMetadataFields) {
Hit<T, K> hit = null;
if (hitElement.isJsonObject()) {
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("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: searchbox-io/Jest
public DateRangeAggregation(String name, JsonObject dateRangeAggregation) {
super(name, dateRangeAggregation);
if (dateRangeAggregation.has(String.valueOf(BUCKETS)) && dateRangeAggregation.get(String.valueOf(BUCKETS)).isJsonArray()) {
parseBuckets(dateRangeAggregation.get(String.valueOf(BUCKETS)).getAsJsonArray());
}
}
代码示例来源:origin: apache/incubator-gobblin
@Test
public void schemaWithUnion()
throws Exception {
String testName = "schemaWithUnion";
JsonObject schema = getSchemaData(testName).getAsJsonObject();
JsonArray expected = getExpectedSchema(testName).getAsJsonArray();
UnionConverter converter = new UnionConverter(new JsonSchema(schema), state);
Assert.assertEquals(avroSchemaToJsonElement(converter), expected);
}
代码示例来源:origin: searchbox-io/Jest
/**
*
* @return empty array if response is not present, otherwise column names as first row plus one additional row per single result
*/
public String[][] getPlainText() {
JsonObject jsonObject = getJsonObject();
if (jsonObject != null && getPathToResult() != null && jsonObject.has(getPathToResult()) && jsonObject.get(getPathToResult()).isJsonArray()) {
JsonArray esResultRows = jsonObject.get(getPathToResult()).getAsJsonArray();
if(esResultRows.size() > 0 && esResultRows.get(0).isJsonObject()) {
return parseResultArray(esResultRows);
}
}
return new String[0][0];
}
代码示例来源:origin: kairosdb/kairosdb
public static boolean isNotNullOrEmpty(ValidationErrors validationErrors, Object name, JsonElement value)
{
if (value == null)
{
validationErrors.addErrorMessage(name + " may not be null.");
return false;
}
if (value.isJsonNull())
{
validationErrors.addErrorMessage(name + " may not be empty.");
return false;
}
if (value.isJsonArray() && value.getAsJsonArray().size() < 1)
{
validationErrors.addErrorMessage(name + " may not be an empty array.");
return false;
}
if (!value.isJsonObject() && value.getAsString().isEmpty())
{
validationErrors.addErrorMessage(name + " may not be empty.");
return false;
}
return true;
}
代码示例来源:origin: apache/incubator-gobblin
private JsonElement getSchemaData(String methodName) {
return testData.get(methodName).getAsJsonArray().get(0);
}
}
代码示例来源:origin: apache/incubator-gobblin
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) + " )");
}
代码示例来源:origin: square/wire
JsonArray array = element.getAsJsonArray();
List<Object> result = new ArrayList<>(array.size());
for (JsonElement item : array) {
result.add(gson.fromJson(item, itemType));
Class<?> valueType = fieldBinding.singleAdapter().javaType;
JsonObject object = element.getAsJsonObject();
Map<Object, Object> result = new LinkedHashMap<>(object.size());
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
代码示例来源:origin: MovingBlocks/Terasology
@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;
}
}
代码示例来源:origin: apache/incubator-gobblin
/**
* Get source.schema within a {@link InputType#RECORD} type.
* The source.schema is represented by a {@link JsonArray}
* @return
*/
public JsonArray getDataTypeValues() {
if (this.type.equals(RECORD)) {
return getDataType().get(RECORD_FIELDS_KEY).getAsJsonArray();
}
return new JsonArray();
}
代码示例来源:origin: EngineHub/WorldEdit
@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);
}
}
代码示例来源:origin: MovingBlocks/Terasology
@Override
public EntityData.Value deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
EntityData.Value.Builder value = EntityData.Value.newBuilder();
if (json.isJsonPrimitive()) {
extractPrimitive(value, json);
} else if (json.isJsonObject()) {
extractMap(json, context, value);
} else if (json.isJsonArray()) {
JsonArray jsonArray = json.getAsJsonArray();
TByteList byteList = new TByteArrayList();
for (JsonElement element : jsonArray) {
if (element.isJsonArray()) {
value.addValue((EntityData.Value) context.deserialize(element, EntityData.Value.class));
} else if (element.isJsonObject()) {
value.addValue((EntityData.Value) context.deserialize(element, EntityData.Value.class));
} else if (element.isJsonPrimitive()) {
extractPrimitive(value, element);
if (element.getAsJsonPrimitive().isNumber()) {
try {
byteList.add(element.getAsByte());
} catch (NumberFormatException nfe) {
byteList.add((byte) 0);
}
}
}
}
value.setBytes(ByteString.copyFrom(byteList.toArray()));
}
return value.build();
}
代码示例来源:origin: chanjarster/weixin-java-tools
@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;
}
代码示例来源:origin: searchbox-io/Jest
public RangeAggregation(String name, JsonObject rangeAggregation) {
super(name, rangeAggregation);
ranges = new ArrayList<Range>();
//todo support keyed:true as well
for (JsonElement bucketElement : rangeAggregation.get(String.valueOf(BUCKETS)).getAsJsonArray()) {
JsonObject bucket = bucketElement.getAsJsonObject();
Range range = new Range(
bucket,
bucket.has(String.valueOf(FROM)) ? bucket.get(String.valueOf(FROM)).getAsDouble() : null,
bucket.has(String.valueOf(TO)) ? bucket.get(String.valueOf(TO)).getAsDouble() : null,
bucket.get(String.valueOf(DOC_COUNT)).getAsLong());
ranges.add(range);
}
}
代码示例来源: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();
代码示例来源:origin: searchbox-io/Jest
public HistogramAggregation(String name, JsonObject histogramAggregation) {
super(name, histogramAggregation);
if(histogramAggregation.has(String.valueOf(BUCKETS)) && histogramAggregation.get(String.valueOf(BUCKETS)).isJsonArray()) {
parseBuckets(histogramAggregation.get(String.valueOf(BUCKETS)).getAsJsonArray());
}
}
内容来源于网络,如有侵权,请联系作者删除!