【问题标题】:Splitting Json array to individual Json elements using Jackson使用 Jackson 将 Json 数组拆分为单个 Json 元素
【发布时间】:2015-04-12 22:34:14
【问题描述】:

有没有办法使用 Jackson 库将给定的 Json 数组拆分为单独的 Json 元素?例如,我有这个 Json 数组:

[
    {
        "key1":"value11", 
        "key2":"value12"
    },
    {
        "key1":"value21", 
        "key2":"value22"
    }
]

拆分后,我想要一个单独的元素列表,例如:

{
        "key1":"value11", 
        "key2":"value12"
}

{
        "key1":"value21", 
        "key2":"value22"
}

【问题讨论】:

    标签: java arrays json jackson


    【解决方案1】:

    这里是一行:

    new ObjectMapper().readTree(json).forEach(node -> System.out.println(node.toString()));
    

    【讨论】:

      【解决方案2】:

      这似乎是作者所要求的。我使用 Jackon 库的 toString 方法将 JSON 列表拆分为两个字符串。

      import com.fasterxml.jackson.databind.JsonNode;
      import com.fasterxml.jackson.databind.ObjectMapper;
      
      ...
      ...
        
      String jsonText = "[{\"test\":1},{\"test2\":2}]";
      int currentElement = 0;
      int elementCount;
      
      ObjectMapper mapper = new ObjectMapper();
      
      JsonNode jsonObj = mapper.readTree(jsonText);
       
      elementCount = jsonObj.size();
      
      while (currentElement<elementCount) {
          System.out.println(jsonObj.get(currentElement).toString());
          currentElement++;
       }
      

      【讨论】:

        【解决方案3】:

        这个问题的一个很好的解决方案是使用 Java 8 Streaming API:s 进行迭代。 JsonNode 对象是 Iterable,其中 spliterator 方法可用。因此,可以使用以下代码:

        public List<String> split(final String jsonArray) throws IOException {
            final JsonNode jsonNode = new ObjectMapper().readTree(jsonArray);
            return StreamSupport.stream(jsonNode.spliterator(), false) // Stream
                    .map(JsonNode::toString) // map to a string
                    .collect(Collectors.toList()); and collect as a List
        }
        

        另一种选择是跳过重新映射(对toString 的调用)并返回List&lt;JsonNode&gt; 元素。这样您就可以使用JsonNode 方法访问数据(getpath 等)。

        【讨论】:

          【解决方案4】:

          最后,我找到了一个可行的解决方案:

          public List<String> split(String jsonArray) throws Exception {
                  List<String> splittedJsonElements = new ArrayList<String>();
                  ObjectMapper jsonMapper = new ObjectMapper();
                  JsonNode jsonNode = jsonMapper.readTree(jsonArray);
          
                  if (jsonNode.isArray()) {
                      ArrayNode arrayNode = (ArrayNode) jsonNode;
                      for (int i = 0; i < arrayNode.size(); i++) {
                          JsonNode individualElement = arrayNode.get(i);
                          splittedJsonElements.add(individualElement.toString());
                      }
                  }
                  return splittedJsonElements;
          }
          

          【讨论】:

            【解决方案5】:

            你可能想看看这个API

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-05-04
              • 2013-06-04
              • 2021-09-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-03-29
              相关资源
              最近更新 更多