java - 我可以将 JSON-LD 转换为 Java 对象吗?
问题描述
编辑:我改变了主意。我会找到一种方法来生成 Java 类并将 JSON 作为该类的对象加载。
我刚刚发现存在JSON
被调用的变体JSON-LD
。
在我看来,这是一种更结构化的定义方式JSON
,它让我想起XML
了一个关联的模式,比如XSD
.
我可以从创建一个 Java 类JSON-LD
,在运行时加载它并使用它来转换JSON-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,如果这对您有任何影响的话。
我与这个项目没有任何关系。我不是作者,也没有提出任何拉取请求。这只是我发现的东西。
祝你好运,我希望这会有所帮助!
推荐阅读
- php - DateTime类创建的时间对象是否受用户机器本地时间的影响?
- c# - 使用 NPOI dll 在 excel 导出中添加货币格式 - 不起作用 - 为什么?
- themes - 如何避免或防止应用程序中的暗模式?
- postgresql - 检查 postgresql 复制
- ios - 如何让 UIAnimation 一次执行一个(遍历 for 循环),而不是一次执行?
- magento2 - 产品导出时的magento 2.3“消息已添加到队列中,请等待尽快获取您的文件”
- java - Elasticsearch RestHighLevelClient SocketTimeoutException
- sql-server - 为单个命令启用 SQL Ad Hoc 分布式查询
- xcode - 使用 Visual Studio 2017 在模拟器上运行 Xamarin.IOS 时修复错误 MT1008
- ios - 数组包含方法无法与自定义模型和谓词一起正常工作