使用 Aeson 读取编码为嵌套字符串的嵌套 JSON 数据

问题描述

我有这个奇怪的 JSON 来解析包含嵌套的 JSON ...一个字符串。所以代替

{\"title\": \"Lord of the rings\",\"author\": {\"666\": \"Tolkien\"}\"}"

我有

{\"title\": \"Lord of the rings\",\"author\": \"{\\\"666\\\": \\\"Tolkien\\\"}\"}"

这是我在 decode 实例中使用 FromJSON 解析嵌套字符串的(失败)尝试:

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Data.Maybe
import GHC.Generics
import Data.Aeson
import qualified Data.Map as M

type Authors = M.Map Int String

data Book = Book
  {
    title :: String,author :: Authors
  }
  deriving (Show,Generic)

decodeAuthors x =  fromJust (decode x :: Maybe Authors)

instance FromJSON Book where
  parseJSON = withObject "Book" $ \v -> do
    t <- v .: "title"
    a <- decodeAuthors <?> v .: "author"
    return  $ Book t a

jsonTest = "{\"title\": \"Lord of the rings\",\"author\": \"{\\\"666\\\": \\\"Tolkien\\\"}\"}"

test = decode jsonTest :: Maybe Book

有没有办法一次性解码整个 JSON?谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)