purescript - 如何在任何字段中使用带有 JSON 的 Data.Argonaut.Decode 模块?
问题描述
我知道我是否有带有特定已知字段的 JSON,例如“日期”和“标题”
{
"date": "any string",
"title": "any string"
}
然后,我可以通过定义来解码它
myDecodeFunc :: Json -> Either JsonDecodeError ({ date :: String, title :: String })
myDecodeFunc = decodeJson
但是,我似乎找不到如何用这样的任意字段解码相同的 JSON。
{
"any field": "any string",
"any field": "any string"
}
我是 Purescript 的新手,所以任何指针都将不胜感激。谢谢。
解决方案
在你事先不知道所有可能字段的“记录”式事物中,一个花哨的术语是dictionary。
并且要将 JSON 解析为字典,请使用字典式类型,Foreign.Object
例如,它恰好有一个方便的实例FromJSON
:
import Foreign.Object (Object)
myDecodeFunc :: Json -> Either JsonDecodeError (Object String)
myDecodeFunc = decodeJson
推荐阅读
- html - 搜索容器不显示
- python - 遍历列表并将列转换为数据时间
- amazon-web-services - 从 AWS lambda 向 IOT 设备发送请求并等待响应
- context-free-grammar - 上下文无关和上下文敏感的语法是什么意思?
- r - 在ggplot2中对齐多线轴标题
- vba - 如何防止一个段落跨越两页
- d3.js - vega-lite:如何将 JSON 中的日期指定为包含年、月和日数的单独字段?
- javascript - 点击被忽略:标签元素与表单控件相关联的问题
- java - 在仍然更改属性的同时重试 Spring Data Flow 任务
- ruby-on-rails - 覆盖模型中的 ActiveSupport::Concern