Yesod - Aeson, "when expecting a Int64, encountered String instead"

Yesod - Aeson, "when expecting a Int64, encountered String instead"

我有以下型号:

Conf
  productTaxRateId ProductTaxRateId Maybe
  barCodeLength Int

我已将以下 json 发送到服务器:

{
  "attributes": {
    "barCodeLength":25
  },
  "relationships": {
    "productTaxRate": {
      "data": {
        "id": "1",
        "type": "ProductTaxRate"
      }
    }
  },
 "id": "1",
 "type": "Conf"
}

以下是我的FromJSON:

instance FromJSON Conf where
  parseJSON (Object o) = Conf 
      <$> ((o .: "relationships") >>= (.: "productTaxRate") >>= (.: "data") >>= (.: "id"))
      <*> ((o .: "attributes") >>= (.: "barCodeLength"))
  parseJSON _ = mzero

但是我的请求出现以下错误:

{"message":"Invalid Arguments","errors":["when expecting a Int64, encountered String instead"]}

如何正确转换?

提前谢谢你,HaskellYesod非常好。

我找到了一种方法,非常简单,我刚刚使用了 fromPathPiece!

instance FromJSON Conf where
  parseJSON (Object o) = Conf 
      <$> fmap fromPathPiece ((o .: "relationships") >>= (.: "productTaxRate") >>= (.: "data") >>= (.: "id"))
      <*> ((o .: "attributes") >>= (.: "barCodeLength"))
  parseJSON _ = mzero