com.fasterxml.jackson.databind.JsonNode.get()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(264)

本文整理了Java中com.fasterxml.jackson.databind.JsonNode.get()方法的一些代码示例,展示了JsonNode.get()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonNode.get()方法的具体详情如下:
包路径:com.fasterxml.jackson.databind.JsonNode
类名称:JsonNode
方法名:get

JsonNode.get介绍

[英]Method for accessing value of the specified element of an array node. For other nodes, null is always returned.

For array nodes, index specifies exact location within array and allows for efficient iteration over child elements (underlying storage is guaranteed to be efficiently indexable, i.e. has random-access to elements). If index is less than 0, or equal-or-greater than node.size(), null is returned; no exception is thrown for any index.

NOTE: if the element value has been explicitly set as null (which is different from removal!), a com.fasterxml.jackson.databind.node.NullNode will be returned, not null.
[中]用于访问数组节点的指定元素的值的方法。对于其他节点,总是返回null。
对于阵列节点,索引指定了阵列中的确切位置,并允许在子元素上进行有效迭代(保证底层存储可以有效地进行索引,即对元素进行随机访问)。如果索引小于0,或等于或大于node.size(),则返回null;任何索引都不会引发异常。
注意:如果元素值已显式设置为null(这与删除不同!),一个com。fasterxml。杰克逊。数据绑定。节点。将返回NullNode,而不是null。

代码示例

代码示例来源:origin: Activiti/Activiti

public static String getStencilId(JsonNode objectNode) {
 String stencilId = null;
 JsonNode stencilNode = objectNode.get(EDITOR_STENCIL);
 if (stencilNode != null && stencilNode.get(EDITOR_STENCIL_ID) != null) {
  stencilId = stencilNode.get(EDITOR_STENCIL_ID).asText();
 }
 return stencilId;
}

代码示例来源:origin: Activiti/Activiti

protected List<String> getValueAsList(String name,
                   JsonNode objectNode) {
  List<String> resultList = new ArrayList<String>();
  JsonNode valuesNode = objectNode.get(name);
  if (valuesNode != null) {
    for (JsonNode valueNode : valuesNode) {
      if (valueNode.get("value") != null && !valueNode.get("value").isNull()) {
        resultList.add(valueNode.get("value").asText());
      }
    }
  }
  return resultList;
}

代码示例来源:origin: joelittlejohn/jsonschema2pojo

private String getTypeName(JsonNode node) {
  if (node.has("type") && node.get("type").isArray() && node.get("type").size() > 0) {
    for (JsonNode jsonNode : node.get("type")) {
      String typeName = jsonNode.asText();
      if (!typeName.equals("null")) {
        return typeName;
      }
    }
  }
  if (node.has("type") && node.get("type").isTextual()) {
    return node.get("type").asText();
  }
  return DEFAULT_TYPE_NAME;
}

代码示例来源:origin: aws/aws-sdk-java

private static List<JmesPathExpression> getChildren(JsonNode jsonNode){
  if(jsonNode.get("children").size() < 1) {
    throw new RuntimeException("Expected one or more arguments");
  }
  Iterator<JsonNode> children = jsonNode.get("children").elements();
  final List<JmesPathExpression> childrenList = new ArrayList<>();
  while (children.hasNext()) {
    childrenList.add(fromAstJsonToAstJava(children.next()));
  }
  return childrenList;
}

代码示例来源:origin: apache/incubator-pinot

private static List<Object> jsonArrayToList(JsonNode jsonArray) {
 List<Object> list = new ArrayList<>();
 for (int i = 0; i < jsonArray.size(); ++i) {
  list.add(jsonArray.get(i));
 }
 return list;
}

代码示例来源:origin: knowm/XChange

private List<YoBitAsksBidsData> parse(JsonNode nodeArray) {
  List<YoBitAsksBidsData> res = new ArrayList<>();
  if (nodeArray != null) {
   for (JsonNode jsonNode : nodeArray) {
    res.add(
      new YoBitAsksBidsData(
        BigDecimal.valueOf(jsonNode.get(1).asDouble()),
        BigDecimal.valueOf(jsonNode.get(0).asDouble())));
   }
  }
  return res;
 }
}

代码示例来源:origin: joelittlejohn/jsonschema2pojo

private ObjectNode objectSchema(JsonNode exampleObject) {
  ObjectNode schema = this.objectMapper.createObjectNode();
  schema.put("type", "object");
  ObjectNode properties = this.objectMapper.createObjectNode();
  for (Iterator<String> iter = exampleObject.fieldNames(); iter.hasNext();) {
    String property = iter.next();
    properties.set(property, schemaFromExample(exampleObject.get(property)));
  }
  schema.set("properties", properties);
  return schema;
}

代码示例来源:origin: auth0/java-jwt

List<String> getStringOrArray(Map<String, JsonNode> tree, String claimName) throws JWTDecodeException {
  JsonNode node = tree.get(claimName);
  if (node == null || node.isNull() || !(node.isArray() || node.isTextual())) {
    return null;
  }
  if (node.isTextual() && !node.asText().isEmpty()) {
    return Collections.singletonList(node.asText());
  }
  List<String> list = new ArrayList<>(node.size());
  for (int i = 0; i < node.size(); i++) {
    try {
      list.add(objectReader.treeToValue(node.get(i), String.class));
    } catch (JsonProcessingException e) {
      throw new JWTDecodeException("Couldn't map the Claim's array contents to String", e);
    }
  }
  return list;
}

代码示例来源:origin: Activiti/Activiti

protected FlowElement convertJsonToElement(JsonNode elementNode, JsonNode modelNode, Map<String, JsonNode> shapeMap) {
 ServiceTask serviceTask = new ServiceTask();
 serviceTask.setType(ServiceTask.DMN_TASK);
 
 JsonNode decisionTableReferenceNode = getProperty(PROPERTY_DECISIONTABLE_REFERENCE, elementNode);
 if (decisionTableReferenceNode != null && decisionTableReferenceNode.has("id") && !(decisionTableReferenceNode.get("id").isNull())) {
  String decisionTableId = decisionTableReferenceNode.get("id").asText();
  if (decisionTableMap != null) {
   String decisionTableKey = decisionTableMap.get(decisionTableId);
   FieldExtension decisionTableKeyField = new FieldExtension();
   decisionTableKeyField.setFieldName(PROPERTY_DECISIONTABLE_REFERENCE_KEY);
   decisionTableKeyField.setStringValue(decisionTableKey);
   serviceTask.getFieldExtensions().add(decisionTableKeyField);
  }
 }
 return serviceTask;
}

代码示例来源:origin: stagemonitor/stagemonitor

@Test
public void reportSpan() throws Exception {
  reporter.report(mock(SpanContextInformation.class), getSpan(100));
  elasticsearchClient.waitForCompletion();
  refresh();
  final JsonNode hits = elasticsearchClient.getJson("/stagemonitor-spans*/_search").get("hits");
  assertThat(hits.get("total").intValue()).as(hits.toString()).isEqualTo(1);
  final JsonNode spanJson = hits.get("hits").elements().next().get("_source");
  assertThat(spanJson.get("type").asText()).as(spanJson.toString()).isEqualTo("jdbc");
  assertThat(spanJson.get("method").asText()).as(spanJson.toString()).isEqualTo("SELECT");
  assertThat(spanJson.get("db.statement")).as(spanJson.toString()).isNotNull();
  assertThat(spanJson.get("db.statement").asText()).as(spanJson.toString()).isEqualTo("SELECT * from STAGEMONITOR where 1 < 2");
  assertThat(spanJson.get("duration_ms").asInt()).as(spanJson.toString()).isEqualTo(100);
  assertThat(spanJson.get("name").asText()).as(spanJson.toString()).isEqualTo("ElasticsearchExternalSpanReporterIntegrationTest#test");
}

代码示例来源:origin: Netflix/eureka

private void doInstanceInfoCompactEncodeDecode(AbstractEurekaJacksonCodec codec, boolean isJson) throws Exception {
  InstanceInfo instanceInfo = infoIterator.next();
  String encodedString = codec.getObjectMapper(InstanceInfo.class).writeValueAsString(instanceInfo);
  if (isJson) {
    JsonNode metadataNode = new ObjectMapper().readTree(encodedString).get("instance").get("metadata");
    assertThat(metadataNode, is(nullValue()));
  }
  InstanceInfo decodedValue = codec.getObjectMapper(InstanceInfo.class).readValue(encodedString, InstanceInfo.class);
  assertThat(decodedValue.getId(), is(equalTo(instanceInfo.getId())));
  assertThat(decodedValue.getMetadata().isEmpty(), is(true));
}

代码示例来源:origin: aws/aws-sdk-java

/**
   * Get a string field from the JSON.
   *
   * @param fieldName Name of field to get.
   * @return String value or null if not present.
   */
  private String getString(String fieldName) {
    return source.has(fieldName) ? source.get(fieldName).asText() : null;
  }
}

代码示例来源:origin: testcontainers/testcontainers-java

private AuthConfig authConfigUsingHelper(final JsonNode config, final String reposName) throws Exception {
  final JsonNode credHelpers = config.get("credHelpers");
  if (credHelpers != null && credHelpers.size() > 0) {
    final JsonNode helperNode = credHelpers.get(reposName);
    if (helperNode != null && helperNode.isTextual()) {
      final String helper = helperNode.asText();
      return runCredentialProvider(reposName, helper);
    }
  }
  return null;
}

代码示例来源:origin: joelittlejohn/jsonschema2pojo

private ObjectNode arraySchema(JsonNode exampleArray) {
  ObjectNode schema = this.objectMapper.createObjectNode();
  schema.put("type", "array");
  if (exampleArray.size() > 0) {
    JsonNode exampleItem = exampleArray.get(0).isObject() ? mergeArrayItems(exampleArray) : exampleArray.get(0);
    schema.set("items", schemaFromExample(exampleItem));
  }
  return schema;
}

代码示例来源:origin: joelittlejohn/jsonschema2pojo

/**
 * Get name of the field generated from property.
 *
 * @param propertyName
 * @param node
 * @return
 */
public String getFieldName(String propertyName, JsonNode node) {
  if (node != null && node.has("javaName")) {
    propertyName = node.get("javaName").textValue();
  }
  return propertyName;
}

代码示例来源:origin: apache/incubator-pinot

private static boolean compareSelection(JsonNode actualJson, JsonNode expectedJson) {
 if (!actualJson.has(SELECTION_RESULTS) && !expectedJson.has(SELECTION_RESULTS)) {
  return true;
 }
 /* We cannot compare numDocsScanned in selection because when we just return part of the selection result (has a
   low limit), this number can change over time. */
 JsonNode actualSelection = actualJson.get(SELECTION_RESULTS);
 JsonNode expectedSelection = expectedJson.get(SELECTION_RESULTS);
 Map<Integer, Integer> expectedToActualColMap = new HashMap<>(actualSelection.get(COLUMNS).size());
 return compareLists(actualSelection.get(COLUMNS), expectedSelection.get(COLUMNS), expectedToActualColMap)
   && compareSelectionRows(actualSelection.get(RESULTS), expectedSelection.get(RESULTS), expectedToActualColMap);
}

代码示例来源:origin: Activiti/Activiti

public static List<JsonNode> getAppModelReferencedProcessModels(JsonNode appModelJson) {
 List<JsonNode> result = new ArrayList<JsonNode>();
 if (appModelJson.has("models")) {
  ArrayNode modelsArrayNode = (ArrayNode) appModelJson.get("models");
  Iterator<JsonNode> modelArrayIterator = modelsArrayNode.iterator();
  while (modelArrayIterator.hasNext()) {
   result.add(modelArrayIterator.next());
  }
 }
 return result;
}

代码示例来源:origin: decaywood/XueQiuSuperSpider

private List<Stock> processNode(JsonNode node) {
  List<Stock> stocks = new ArrayList<>();
  JsonNode data = node.get("data");
  for (JsonNode jsonNode : data) {
    String symbol = jsonNode.get(0).asText();
    String name = jsonNode.get(1).asText();
    Stock stock = new Stock(name, symbol);
    stocks.add(stock);
  }
  return stocks;
}

代码示例来源:origin: twosigma/beakerx

@Override
public Object deserialize(JsonNode n, ObjectMapper mapper) {
 List<Object> o = new ArrayList<Object>();
 try {
  logger.debug("using custom array deserializer");
  for(int i=0; i<n.size(); i++) {
   o.add(parent.deserialize(n.get(i), mapper));
  }
 } catch (Exception e) {
  logger.error("exception deserializing Collection ", e);
  o = null;
 }
 return o;
}

代码示例来源:origin: galenframework/galen

public List<PageItem> loadItems(InputStream stream) throws IOException {
  JsonNode jsonTree = mapper.readTree(stream);
  List<PageItem> items = new LinkedList<>();
  jsonTree.get("items").fields().forEachRemaining(itemEntry -> {
    items.add(new PageItem(itemEntry.getKey(), readArea(itemEntry.getValue())));
  });
  return items;
}

相关文章