我是Haskell的新手。目前我正在做一个SQL项目,我在解析JSON文件内容时卡住了。我得到了一些该死的错误,我不知道如何修复它。
这是我的JSON文件:
{
"Table":"flags",
"Columns":[
{"Name":"flag", "ColumnType":"StringType"},
{"Name":"value", "ColumnType":"BoolType"}],
"Rows":[
[{"Value":"StringValue a"}, {"Value":"BoolValue True"}],
[{"Value":"StringValue b"}, {"Value":"BoolValue True"}],
[{"Value":"StringValue b"}, {"Value":"NullValue"}],
[{"Value":"StringValue b"}, {"Value":"BoolValue False"}]
]
}
字符串
这是我的Haskell代码:
data FromJSONColumn = FromJSONColumn {
deserializedName :: String,
deserializedDataType :: String
} deriving (Show, Eq, Generic)
instance FromJSON FromJSONColumn where
parseJSON (Object v) =
FromJSONColumn <$> v .: "Name"
<*> v .: "ColumnType"
parseJSON _ = mzero
型
这是我得到的错误:
JSONExample.hs:17:27-32: error:
• Couldn't match type ‘[Char]’ with ‘Key’
Expected: Key
Actual: String
• In the second argument of ‘(.:)’, namely ‘"Name"’
In the second argument of ‘(<$>)’, namely ‘v .: "Name"’
In the first argument of ‘(<*>)’, namely
‘FromJSONColumn <$> v .: "Name"’
|
17 | FromJSONColumn <$> v .: "Name"
| ^^^^^^
JSONExample.hs:18:12-23: error:
• Couldn't match type ‘[Char]’ with ‘Key’
Expected: Key
Actual: String
• In the second argument of ‘(.:)’, namely ‘"ColumnType"’
In the second argument of ‘(<*>)’, namely ‘v .: "ColumnType"’
In the expression:
FromJSONColumn <$> v .: "Name" <*> v .: "ColumnType"
|
18 | <*> v .: "ColumnType"
| ^^^^^^^^^^^^
型
谁能解释一下如何为这样的文件(包含表的文件)写示例?我该怎么做?我真的迷路了。
P.S.这是我正在使用的数据类型:
data ColumnType
= IntegerType
| StringType
| BoolType
deriving (Show, Eq)
data Column = Column String ColumnType
deriving (Show, Eq)
data Value
= IntegerValue Integer
| StringValue String
| BoolValue Bool
| NullValue
deriving (Show, Eq)
type Row = [Value]
data DataFrame = DataFrame [Column] [Row]
deriving (Show, Eq)
型
1条答案
按热度按时间fnvucqvd1#
.:
运算符的第二个参数应该是Key
类型,但您传入的是[Char]
(也称为String
)。尝试以下方法之一将它们转换为Key
:1.启用
OverloadedStrings
扩展。即,将{-# LANGUAGE OverloadedStrings #-}
添加到源代码的顶部。1.用
fromString "Name"
代替"Name"
。同样,用fromString "ColumnType"
代替"ColumnType"
。fromString
应该从Data.Aeson.Key模块导入。这实际上和第一个选项做的是一样的,但是OverloadedStrings
扩展可以为你做这件事。