用Haskell Aeson解析数组 [英] Parsing an Array with Haskell Aeson

查看:180
本文介绍了用Haskell Aeson解析数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {series:[[1,2],[2] ,3],[3,4]]} 

我想将其解析为一组数据类型:

  data Series = Series [DataPoint] 
data DataPoint = DataPoint Int Int - x和y

我有很多问题试图写 FromJSON b

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

我试过使用Lens来破坏DataPoint记录,但它不能编译:

  case a ^ .. values。 _的整数 - } 
[x,y] - > DataPoint< $> x * y
_ - > mzero

这个错误会失败(前两行我甚至没有镜头诡计,只是尝试创建 DataPoint< $> 1> 2< / code>):

 无法匹配类型'aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser 
整数'
'整数'
预期类型:(aeson- 0.7.0.6:Data.Aeson.Types.Internal.Parser
整数
- >常量
(Data.Monoid.Endo
[aeson-0.7.0.6:Data.Aeson .Types.Internal.Parse
(aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser I
- >值
- > Const
(Data。 Monoid.Endo
[aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser

实际类型:(整数
- >常量
(Data.Monoid.Endo
[aeson-0.7.0.6:Data.Aeson.Types.Internal.Parse
Integer]
- >价值
- > Const。
(Data.Monoid.Endo
[aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser
Value
在'(。)'的第二个参数中, ,'_Integer'
在'(^ ..)'的第二个参数中,即'values。_Integer'

有没有更好的方法来做到这一点?

有没有人解析数组值变成更详细的结构? 处理向量。

  { - #LANGUAGE LambdaCase# - } 
导入Data.Aeson

data Series = Series [DataPoint]
data DataPoint = DataPoint Int Int

instance FromJSON DataPoint where
parseJSON jsn = do
[x,y]< - parseJSON jsn
返回$ DataPoint xy

实例FromJSON系列其中
parseJSON = \ case
Object o - > (o。:series)>> = fmap Series。 parseJSON
x - >失败$意外的json:++显示x


I have a JSON doc that looks like:

{ "series": [[1,2], [2,3], [3,4]] }

I'd like to parse this into a set of data types:

data Series = Series [DataPoint]
data DataPoint = DataPoint Int Int  -- x and y

I'm having lots of problems trying to write the FromJSON instance for DataPoint.

instance FromJSON DataPoint where
  parseJSON (Array a) = ???

I've tried using Lens to destruct the DataPoint record, but it doesn't compile:

case a ^.. values . _Integer of -}
  [x,y] -> DataPoint <$> x <*> y
  _     -> mzero

That fails with this error (the first two lines I get even absent the lens trickery, just trying to create a DataPoint <$> 1 <*> 2):

Couldn't match type ‘aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser
                       Integer’
              with ‘Integer’
Expected type: (aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser
                  Integer
                -> Const
                     (Data.Monoid.Endo
                        [aeson-0.7.0.6:Data.Aeson.Types.Internal.Parse
                     (aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser I
               -> Value
               -> Const
                    (Data.Monoid.Endo
                       [aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser
                    Value
  Actual type: (Integer
                -> Const
                     (Data.Monoid.Endo
                        [aeson-0.7.0.6:Data.Aeson.Types.Internal.Parse
                     Integer)
               -> Value
               -> Const
                    (Data.Monoid.Endo
                       [aeson-0.7.0.6:Data.Aeson.Types.Internal.Parser
                    Value
In the second argument of ‘(.)’, namely ‘_Integer’
In the second argument of ‘(^..)’, namely ‘values . _Integer’

Is there a better way to do this?

Does anybody have an example of parsing arrays of values into a more detailed structure?

解决方案

Aeson have instance for list, so I think it is not necessary to deal with vectors.

{-# LANGUAGE LambdaCase #-}
import Data.Aeson

data Series = Series [DataPoint]
data DataPoint = DataPoint Int Int

instance FromJSON DataPoint where
  parseJSON jsn = do
    [x,y] <- parseJSON jsn
    return $ DataPoint x y

instance FromJSON Series where
  parseJSON = \case
    Object o -> (o .: "series") >>= fmap Series . parseJSON
    x -> fail $ "unexpected json: " ++ show x

这篇关于用Haskell Aeson解析数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆