首页 > 解决方案 > 我可以将 JSON-LD 转换为 Java 对象吗?

问题描述

编辑:我改变了主意。我会找到一种方法来生成 Java 类并将 JSON 作为该类的对象加载。


我刚刚发现存在JSON被调用的变体JSON-LD

在我看来,这是一种更结构化的定义方式JSON,它让我想起XML了一个关联的模式,比如XSD.

我可以从创建一个 Java 类JSON-LD,在运行时加载它并使用它来转换JSON-LD为该类的实例化吗?

我阅读了这两种实现的文档,但我一无所获。也许我读错了?

标签: javajsonjenajson-ld

解决方案


做一个谷歌搜索把我带到了一个库,它将把 JSON-LD 解码为 "undefined" Object

// 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));

https://github.com/jsonld-java/jsonld-java

显然,它也可以从一个字符串中获取它,就像从文件或其他来源中读取它一样。你如何访问的内容object,我不知道。不过,文档似乎还算不错。

这似乎是一个活跃的项目,因为最后一次提交仅在 4 天前,并且有 30 个贡献者。许可证是 BSD 3-Clause,如果这对您有任何影响的话。

我与这个项目没有任何关系。我不是作者,也没有提出任何拉取请求。这只是我发现的东西。

祝你好运,我希望这会有所帮助!


推荐阅读