首页 > 解决方案 > 如何在任何字段中使用带有 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 的新手,所以任何指针都将不胜感激。谢谢。

标签: purescript

解决方案


在你事先不知道所有可能字段的“记录”式事物中,一个花哨的术语是dictionary

并且要将 JSON 解析为字典,请使用字典式类型,Foreign.Object例如,它恰好有一个方便的实例FromJSON

import Foreign.Object (Object)

myDecodeFunc :: Json -> Either JsonDecodeError (Object String)
myDecodeFunc = decodeJson 

推荐阅读