我如何用 Aeson 解析这个 JSON?
How do I parse this JSON with Aeson?
我有以下 JSON 片段:
{
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04n"
}
],
"main": {
"temp": 271.979,
"pressure": 1024.8,
"humidity": 100,
"temp_min": 271.979,
"temp_max": 271.979,
"sea_level": 1028.51,
"grnd_level": 1024.8
},
"id": 6332485,
"name": "Queensbridge Houses",
"cod": 200
}
我想从中解析以下类型:
data WeatherResponse = WeatherResponse
{ temp :: Double
, humidity :: Double
, weatherMain :: T.Text
} deriving Show
我一直在尝试使用下面的代码来完成,但我一直 运行 出错。我终于找到了所有匹配的类型,但它解析不正确并且不太明白它失败的地方。
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Aeson
import Data.Aeson.Types (Parser, Array)
import Data.Time (defaultTimeLocale, formatTime, getZonedTime)
import qualified Data.ByteString.Lazy as BL
import qualified Data.Vector as V
import qualified Data.Text as T
data WeatherResponse = WeatherResponse
{ temp :: Double
, humidity :: Double
, weatherMain :: T.Text
} deriving Show
lambda3 :: Value -> Parser T.Text
lambda3 o = do
withText "main" (\t -> do
return t
) o
parseInner :: Value -> Parser T.Text
parseInner a = withArray "Inner Array" (lambda3 . (V.head)) a
instance FromJSON WeatherResponse where
parseJSON =
withObject "Root Object" $ \o -> do
mainO <- o .: "main"
temp <- mainO .: "temp"
humidity <- mainO .: "humidity"
weatherO <- o .: "weather"
weatherMain <- parseInner weatherO
return $ WeatherResponse temp humidity weatherMain
getSampleData = BL.readFile "/home/vmadiath/.xmonad/weather.json"
main = do
text <- getSampleData
let (result :: Either String WeatherResponse) = eitherDecode text
putStrLn . show $ result
我只是得到以下输出,这不足以让我知道哪里出了问题。
$ runhaskell lib/code.hs
Left "Error in $: expected main, encountered Object"
我已将整个内容放在一个可见的要点中 here
我想知道代码有什么问题,以及如何修复它。如果您有关于如何以更具可读性的方式编写本文的建议,我也很想知道。目前我主要对两个单独的函数感到恼火 lambda3
和 parseInner
很烦人)
在我看来,你把事情搞得太复杂了。这样的事情应该有效:
instance FromJSON WeatherResponse where
parseJSON (Object v) = do
weatherValue <- head <$> v .: "weather"
WeatherResponse <$> ((v .: "main") >>= (.: "temp"))
<*> ((v .: "main") >>= (.: "humidity"))
<*> weatherValue .: "main"
输出:
[nix-shell:~/haskell-sample]$ ./weather
Right (WeatherResponse {temp = 271.979, humidity = 100.0, weatherMain = "Clouds"})
我有以下 JSON 片段:
{
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04n"
}
],
"main": {
"temp": 271.979,
"pressure": 1024.8,
"humidity": 100,
"temp_min": 271.979,
"temp_max": 271.979,
"sea_level": 1028.51,
"grnd_level": 1024.8
},
"id": 6332485,
"name": "Queensbridge Houses",
"cod": 200
}
我想从中解析以下类型:
data WeatherResponse = WeatherResponse
{ temp :: Double
, humidity :: Double
, weatherMain :: T.Text
} deriving Show
我一直在尝试使用下面的代码来完成,但我一直 运行 出错。我终于找到了所有匹配的类型,但它解析不正确并且不太明白它失败的地方。
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Aeson
import Data.Aeson.Types (Parser, Array)
import Data.Time (defaultTimeLocale, formatTime, getZonedTime)
import qualified Data.ByteString.Lazy as BL
import qualified Data.Vector as V
import qualified Data.Text as T
data WeatherResponse = WeatherResponse
{ temp :: Double
, humidity :: Double
, weatherMain :: T.Text
} deriving Show
lambda3 :: Value -> Parser T.Text
lambda3 o = do
withText "main" (\t -> do
return t
) o
parseInner :: Value -> Parser T.Text
parseInner a = withArray "Inner Array" (lambda3 . (V.head)) a
instance FromJSON WeatherResponse where
parseJSON =
withObject "Root Object" $ \o -> do
mainO <- o .: "main"
temp <- mainO .: "temp"
humidity <- mainO .: "humidity"
weatherO <- o .: "weather"
weatherMain <- parseInner weatherO
return $ WeatherResponse temp humidity weatherMain
getSampleData = BL.readFile "/home/vmadiath/.xmonad/weather.json"
main = do
text <- getSampleData
let (result :: Either String WeatherResponse) = eitherDecode text
putStrLn . show $ result
我只是得到以下输出,这不足以让我知道哪里出了问题。
$ runhaskell lib/code.hs
Left "Error in $: expected main, encountered Object"
我已将整个内容放在一个可见的要点中 here
我想知道代码有什么问题,以及如何修复它。如果您有关于如何以更具可读性的方式编写本文的建议,我也很想知道。目前我主要对两个单独的函数感到恼火 lambda3
和 parseInner
很烦人)
在我看来,你把事情搞得太复杂了。这样的事情应该有效:
instance FromJSON WeatherResponse where
parseJSON (Object v) = do
weatherValue <- head <$> v .: "weather"
WeatherResponse <$> ((v .: "main") >>= (.: "temp"))
<*> ((v .: "main") >>= (.: "humidity"))
<*> weatherValue .: "main"
输出:
[nix-shell:~/haskell-sample]$ ./weather
Right (WeatherResponse {temp = 271.979, humidity = 100.0, weatherMain = "Clouds"})