如果您阅读了您链接到的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(此对象的类型将是List、Map、String、Boolean、Number 或null,具体取决于根文件中的对象)。
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");