【问题标题】:How to traverse, navigate and access objects from a JSON-LD files with JSONLD-JAVA?如何使用 JSONLD-JAVA 遍历、导航和访问 JSON-LD 文件中的对象?
【发布时间】:2018-04-08 06:39:48
【问题描述】:

我需要将一个大型 JSON-LD 文件作为我用 Java 编写的算法的输入。因此,我打算为此使用 JSONLD-JAVA。

JSONLD-JAVA 页面显示了一个读取 JSON-LD 文件的示例,但不适用于导航或遍历它,或访问其中的单个对象。相反,它指的是JSON-LDJSON-LD API 规范,以了解有关具体可能操作的详细信息。

但是,JSON-LD 规范只是简单地定义了 JSON-LD 的语法和语义,并没有说明如何访问它们,当然也不应该,它只是格式的规范。我期待在 JSON-LD API 规范中描述这种操作,但它只描述了将整个 JSON-LD 文件转换为不同形式(压缩、扩展、扁平化和转换为 RDF)的操作。它似乎不包括访问对象的操作(例如,访问对象的键值对)。

所以我猜我们应该读取 JSON-LD 文件并展开或展平它,然后将其作为纯 JSON 访问。但是 JSONLD-JAVA 方法只返回 Object 的实例,所以我不清楚如何使用这些对象来获取 JSON 键值对。唯一的例外似乎是方法frame,它返回一个Map,但我不太清楚什么是框架。 JSON-LD 规范不包含“框架”一词,JSON-LD API 规范有一个非常简洁的解释,这似乎无助于理解如何访问对象的键值对。

我只有 JSONLD-JAVA 方法中的 Object 实例这一事实也使得使用一些 JSON 库来使用它们看起来很困难,除非我使用一些知道这些对象内部格式的 JSON 库作为由 JSONLD-JAVA 组成,但是 JSONLD-Java 的页面没有提到任何这样的库。

我希望能够读取 JSON-LD 文件,然后在 Java 中以编程方式访问或操作它,并拥有与主要概念相对应的 Java 类,例如 JSONLDObject 以及提供其密钥的方法-值对。

当我阅读以上页面时,我觉得它们是为那些已经知道我不知道的东西的人准备的。所以也许我错过了一些东西。否则,是否有关于使用 JSONLD-JAVA 甚至只是 JSONLD API 来遍历对象的教程?

【问题讨论】:

    标签: java json json-ld


    【解决方案1】:

    如果您阅读了您链接到的JSONLD-JAVA 页面上的文档,它会以注释 示例开头:

    // Open a valid json(-ld) input file
    InputStream inputStream = new FileInputStream("input.json");
    // Read the file into an Object (The type of this object will be a List, Map, String, Boolean,
    // Number or null depending on the root object in the file).
    Object jsonObject = JsonUtils.fromInputStream(inputStream);
    // Create a context JSON map containing prefixes and definitions
    Map context = new HashMap();
    // Customise context...
    // Create an instance of JsonLdOptions with the standard JSON-LD options
    JsonLdOptions options = new JsonLdOptions();
    // Customise options...
    // Call whichever JSONLD function you want! (e.g. compact)
    Object compact = JsonLdProcessor.compact(jsonObject, context, options);
    // Print out the result (or don't, it's your call!)
    System.out.println(JsonUtils.toPrettyString(compact));
    

    第二条评论很有趣,所以让我为你强调一下:

    将文件读入Object(此对象的类型将是ListMapStringBooleanNumbernull,具体取决于根文件中的对象)。

    Object jsonObject = JsonUtils.fromInputStream(inputStream);
    

    关键是 JSONLD 是 JSON,当你像上面那样将它加载到内存中时,你可以通过转换 @ 来导航那个 JSON 结构987654332@视情况而定。

    让我们看看来自JSON-LD specification 的示例#3:

    {
      "@context":
      {
        "name": "http://schema.org/name",  // ← This means that 'name' is shorthand for 'http://schema.org/name' 
        "image": {
          "@id": "http://schema.org/image",  // ← This means that 'image' is shorthand for 'http://schema.org/image' 
          "@type": "@id"  // ← This means that a string value associated with 'image' should be interpreted as an identifier that is an IRI 
        },
        "homepage": {
          "@id": "http://schema.org/url",  // ← This means that 'homepage' is shorthand for 'http://schema.org/url' 
          "@type": "@id"  // ← This means that a string value associated with 'homepage' should be interpreted as an identifier that is an IRI 
        }
      }
    }
    

    因此,如果您想要 image@id 值,您可以这样做:

    Map<String, Object> root = (Map) jsonObject;
    Map<String, Object> context = (Map) root.get("@context");
    Map<String, Object> image = (Map) root.get("image");
    String imageId = (String) image.get("@id");
    

    【讨论】:

      【解决方案2】:

      1. 将 JSON-LD 转换为漂亮的嵌套地图。使用框架算法。示例:JSON-LD to normal JSONHow to convert RDF to pretty nested JSON using java rdf4j

      2. 访问 JSON-LD。我会使用JsonNodeJPointer。 对小而简单的文件直接在Map&lt;String,Object&gt;上操作也可以。对于 JsonPointer,您可以使用 Jackson JsonNode.at()

      ObjectMapper mapper = new ObjectMapper();
      JsonNode json = mapper.readValue(in, JsonNode.class);
      String id = json.at("/@id").getText();
      

      3. 预处理。在某些情况下,预处理 JSON 输入会很方便。这个答案列出了一些命令行工具:XSLT equivalent for JSON

      【讨论】:

        猜你喜欢
        • 2015-02-27
        • 1970-01-01
        • 2022-11-21
        • 1970-01-01
        • 2016-05-20
        • 1970-01-01
        • 1970-01-01
        • 2019-06-07
        • 1970-01-01
        相关资源
        最近更新 更多