猿问

如何在 JSON 中获取子元素的父元素名称?

我有一些动态 JSON,在处理之前我不知道完整的结构。


但是,我知道 JSON 可能包含某些感兴趣的嵌套元素。


e. 对于这个示例有效载荷



{

  "id": "3334343",

  "contractor": {

    "ppsNo": "123334"

  },

  "fullTimeStaff":{

    "ppsNo": "343434"

  }

}

我想找到包含名为ppsNo的元素的所有外部元素的名称。


我曾尝试使用root.findParents("ppsNo")但这给了我 ppsNo 元素,而不是我感兴趣的承包商和fullTimeStaff的外部(父)元素。


    ObjectMapper objectMapper = new ObjectMapper();

    String payload = "{\n" +

            "  \"id\": \"3334343\",\n" +

            "  \"contractor\": {\n" +

            "    \"ppsNo\": \"123334\"\n" +

            "  },\n" +

            "  \"fullTimeStaff\":{\n" +

            "    \"ppsNo\": \"123334\"\n" +

            "  }\n" +

            "}";


    JsonNode root = objectMapper.readTree(payload);


    List<JsonNode> nodes = root.findParents("ppsNo");

返回的 JsonNodes 是{"ppsNo":"123334"}元素而不是外部包含节点(“contractor”和“fullTimeStaff”)。


有没有办法做到这一点?我曾研究过使用 JSON 路径,但我看不到一种明显的方法来使用它来获取真正的父(包含/外部)元素。


我在这个例子中使用杰克逊,但我对替代品持开放态度


慕后森
浏览 247回答 3
3回答

慕少森

JsonPath 库有一个选项,它返回整个匹配的路径而不是值。因此,您可以执行以下操作:Configuration conf = Configuration.builder().options(Option.AS_PATH_LIST).build();List<String> pathList = JsonPath.using(conf).parse(payload).read("$..ppsNo");/* Returns :&nbsp;* [&nbsp;*&nbsp; &nbsp;"$['contractor']['ppsNo']",&nbsp;*&nbsp; &nbsp;"$['fullTimeStaff']['ppsNo']"&nbsp;* ]&nbsp;*/您只需将结果解析为正确的类型,然后删除最后一个元素即可获得直接父级。Pattern pattern = Pattern.compile("(?<!\\$\\[)(\\w+)(?!\\])");pathList = pathList.stream().map(path -> {&nbsp; &nbsp; Matcher m = pattern.matcher(path.toString());&nbsp; &nbsp; return m.find() ? m.group(0) : null;}).collect(Collectors.toList());System.out.println(pathList); // [contractor, fullTimeStaff]这是官方Jayway JsonPath Maven 存储库的链接。

慕田峪9158850

我没有花很多时间来编写代码。您正在寻找的表达式Regex是\{\n &nbsp; &nbsp;"ppsNo": "\w+"\n &nbsp;}(您使用链接https://regex101.com对其进行测试)。你应该做的是当你遇到这个表达式时开始倒退并阅读“”中的第一个单词。我希望它对你有帮助

慕勒3428872

我们可以在JsonNode.fields方法的帮助下使用递归来搜索目标父节点,如下例所示。public class GetParentByChildName {&nbsp; &nbsp; public static void main(String[] args) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper objectMapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; String payload = "{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; \"id\": \"3334343\",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; \"contractor\": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; \"ppsNo\": \"123334\"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " \"arr\": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; \"contractor2\": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; &nbsp; \"ppsNo\": \"123334\"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; &nbsp; \"contractor3\": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"ppsNo\": \"123334\"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; \"fullTimeStaff\":{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; \"ppsNo\": \"123334\"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;";&nbsp; &nbsp; &nbsp; &nbsp; JsonNode root = objectMapper.readTree(payload);&nbsp; &nbsp; &nbsp; &nbsp; List<String> fieldNames = new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; getParentName(root, "ppsNo", fieldNames);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(fieldNames);&nbsp; &nbsp; }&nbsp; &nbsp; private static void getParentName(JsonNode node, String targetChildName, List<String> fieldNames) {&nbsp; &nbsp; &nbsp; &nbsp; if (node.getNodeType() == JsonNodeType.ARRAY) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node.elements().forEachRemaining(x -> getParentName(x, targetChildName, fieldNames));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (node.getNodeType() != JsonNodeType.OBJECT) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; node.fields().forEachRemaining(x -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Iterator<String> iter = x.getValue().fieldNames();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (iter.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fieldName = iter.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (fieldName.equals(targetChildName)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldNames.add(x.getKey());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getParentName(x.getValue(), targetChildName, fieldNames);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答