[英]How to traverse, navigate and access objects from a JSON-LD files with JSONLD-JAVA?
I need to take a large JSON-LD file as input for an algorithm that I am writing in Java. 我需要使用一个大的JSON-LD文件作为我用Java编写的算法的输入。 Therefore, I intend to use JSONLD-JAVA for that.
因此,我打算为此使用JSONLD-JAVA。
The JSONLD-JAVA page shows an example for reading a JSON-LD file, but not for navigating or traversing it, or accessing individual objects in it. JSONLD-JAVA页面显示了一个读取JSON-LD文件,而不是浏览或遍历该文件或访问其中的单个对象的示例。 Instead, it refers to the JSON-LD and JSON-LD API specifications for details on specific possible operations.
相反,它参考JSON-LD和JSON-LD API规范以获取有关特定可能操作的详细信息。
However, the JSON-LD specification simply defines the syntax and semantics of a JSON-LD, and does not say anything about how to access them, and of course neither should it, being just a specification of the format. 但是,JSON-LD规范仅定义了JSON-LD的语法和语义,没有说明如何访问它们,当然也不应该仅仅是格式的规范。 I was expecting that kind of operation to be described in the JSON-LD API specification, but it only describes operations that convert the entire JSON-LD file into different forms (compact, expanded, flattening, and conversion to RDF).
我期望在JSON-LD API规范中描述这种操作,但它仅描述将整个JSON-LD文件转换为不同形式(紧凑,扩展,展平以及转换为RDF)的操作。 It does not seem to include operations for accessing the objects (for example, accessing the key-value pairs of an object).
它似乎不包含用于访问对象的操作(例如,访问对象的键值对)。
So I am guessing we are supposed to read the JSON-LD file and expand or flatten it, and then access it as pure JSON. 因此,我猜测我们应该读取JSON-LD文件并对其进行扩展或展平,然后将其作为纯JSON访问。 But JSONLD-JAVA methods only return instances of
Object
, so it's not clear to me how I can use these object to obtain the JSON key-value pairs. 但是JSONLD-JAVA方法仅返回
Object
实例,因此我不清楚如何使用这些对象来获取JSON键值对。 The only exception seems to be the method frame
, which returns a Map, but it is not very clear to me what a frame is. 唯一的例外似乎是方法
frame
,该方法返回一个Map,但是对我来说不是很清楚什么是框架。 The JSON-LD specification does not include the word "frame", and the JSON-LD API specification has a very terse explanation which does not seem to help in understanding how to access an object's key-value pairs. JSON-LD规范不包含“框架”一词,并且JSON-LD API规范具有非常简洁的解释,这似乎无助于理解如何访问对象的键值对。
The fact that I only have Object instances from JSONLD-JAVA methods also makes it look like it would be hard to use some JSON library to use them, unless I use some JSON library that knows about the internal format of these objects as formed by JSONLD-JAVA, but the page of JSONLD-Java does not mention any such library. 我只有JSONLD-JAVA方法中的Object实例的事实也使得看起来似乎很难使用某些JSON库来使用它们,除非我使用一些了解JSONLD形成的这些对象的内部格式的JSON库-JAVA,但JSONLD-Java的页面未提及任何此类库。
I was expecting to be able to read a JSON-LD file and then programmatically accessing or manipulating it within Java, and to have Java classes that correspond to the main concepts, something like a JSONLDObject
with methods for providing its key-value pairs. 我期望能够读取JSON-LD文件,然后在Java中以编程方式访问或操作它,并拥有与主要概念相对应的Java类,例如
JSONLDObject
,提供用于提供其键值对的方法。
As I read the above pages, I get the feeling that they are meant for people that already know something that I don't. 当我阅读以上页面时,我感到它们是为那些已经知道我不了解的人的。 So perhaps I am missing something.
所以也许我缺少一些东西。 Otherwise, is there a tutorial on using JSONLD-JAVA or even just the JSONLD API in order to traverse the objects?
否则,是否有使用JSONLD-JAVA甚至只是JSONLD API来遍历对象的教程?
If you read the documentation on the JSONLD-JAVA page you linked to, it starts with a commented example: 如果您阅读了链接到的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));
The second comment is interesting, so let me highlight it for you: 第二条评论很有趣,所以让我为您重点介绍一下:
Read the file into an
Object
(The type of this object will be aList
,Map
,String
,Boolean
,Number
ornull
depending on the root object in the file).将文件读入一个
Object
(此对象的类型为List
,Map
,String
,Boolean
,Number
或null
具体取决于文件中的根对象)。Object jsonObject = JsonUtils.fromInputStream(inputStream);
The key point is that JSONLD is JSON , and that when you've loaded it into memory like above, you can navigate that JSON structure, by casting the Object
as appropriate. 关键是JSONLD是JSON ,并且当您将其像上面那样加载到内存中时,可以通过适当地转换
Object
来导航该JSON结构。
Lets have a look at Example #3 from the JSON-LD specification : 让我们看一下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
}
}
}
So if you want the @id
value of the image
, you'd do this: 因此,如果您想要
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. Convert JSON-LD to a nice nested map. 1.将JSON-LD转换为漂亮的嵌套地图。 Use the framing algorithm.
使用成帧算法。 Example: JSON-LD to normal JSON and How to convert RDF to pretty nested JSON using java rdf4j
示例:将JSON-LD 转换 为普通JSON,以及如何使用Java rdf4j将RDF转换为漂亮的嵌套JSON
2. Accessing JSON-LD. 2.访问JSON-LD。 I would use JsonNode together with JPointer .
我将JsonNode和JPointer一起使用。 On small and simple documents operating directly on the
Map<String,Object>
is also ok. 在直接在
Map<String,Object>
上操作的小型文档上也可以。 For JsonPointer you can use Jackson JsonNode.at() . 对于JsonPointer,您可以使用Jackson JsonNode.at() 。
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readValue(in, JsonNode.class);
String id = json.at("/@id").getText();
3. Preprocessing. 3.预处理。 In some cases it can be handy to preprocess the JSON Input.
在某些情况下,预处理JSON输入可能很方便。 This answer lists some command line tools: XSLT equivalent for JSON
此答案列出了一些命令行工具: JSON的XSLT等效项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.