编辑:我改变主意了。我会找到一种方法来生成 Java 类并将 JSON 作为该类的对象加载。
我刚刚发现存在 JSON
的变体称为 JSON-LD
.
在我看来,这是一种更加结构化的方式来定义 JSON
,这让我想起了XML
具有关联的架构,例如 XSD
.
我可以从 JSON-LD
创建一个 Java 类吗? ,在运行时加载它并用它来转换 JSON-LD
到那个类的实例化?
我阅读了这两种实现的文档,但一无所获。也许我读得不好?
最佳答案
通过 Google 搜索,我找到了一个将 JSON-LD 解码为“未定义”对象
的库。
// 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
显然,它也可以只从字符串中获取它,就好像从文件或其他来源中读取它一样。你如何访问对象
的内容,我不知道。不过,该文档似乎还算不错。
这似乎是一个活跃的项目,因为最后一次提交是在 4 天前,并且有 30 个贡献者。许可证是 BSD 3-Clause,如果这对您有任何影响的话。
我与该项目没有任何关联。我不是作者,也没有提出任何请求请求。这只是我发现的东西。
祝你好运,我希望这对你有所帮助!
关于java - 我可以将 JSON-LD 转换为 Java 对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57206944/