簡體   English   中英

如何使用JSONLD-JAVA從JSON-LD文件中遍歷,導航和訪問對象?

[英]How to traverse, navigate and access objects from a JSON-LD files with JSONLD-JAVA?

我需要使用一個大的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庫來使用它們,除非我使用一些了解JSONLD形成的這些對象的內部格式的JSON庫-JAVA,但JSONLD-Java的頁面未提及任何此類庫。

我期望能夠讀取JSON-LD文件,然后在Java中以編程方式訪問或操作它,並擁有與主要概念相對應的Java類,例如JSONLDObject ,提供用於提供其鍵值對的方法。

當我閱讀以上頁面時,我感到它們是為那些已經知道我不了解的人的。 所以也許我缺少一些東西。 否則,是否有使用JSONLD-JAVA甚至只是JSONLD API來遍歷對象的教程?

如果您閱讀了鏈接到的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 ,並且當您將其像上面那樣加載到內存中時,可以通過適當地轉換Object來導航該JSON結構。

讓我們看一下JSON-LD規范中的示例#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");

1.將JSON-LD轉換為漂亮的嵌套地圖。 使用成幀算法。 示例:將JSON-LD 轉換 為普通JSON,以及如何使用Java rdf4j將RDF轉換為漂亮的嵌套JSON

2.訪問JSON-LD。 我將JsonNodeJPointer一起使用。 在直接在Map<String,Object>上操作的小型文檔上也可以。 對於JsonPointer,您可以使用Jackson JsonNode.at()

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

3.預處理。 在某些情況下,預處理JSON輸入可能很方便。 此答案列出了一些命令行工具: JSON的XSLT等效項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM