如何从Json file.Json 结构中提取信息必须通过类型映射并使用AESON
How to extract information from Json file.Json structure must be mapped through type and use AESON
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
module Main where
import Data.Aeson
import Data.Aeson.Encode.Pretty
import Data.Aeson.Types
import Data.ByteString as B
import qualified Data.ByteString.Lazy.Char8 as LC
import GHC.Generics
import Data.Text
data MainWeatherInfo = MainWeatherInfo
{ mainInfo :: Main
} deriving (Show,Generic)
data Main = Main
{ temp :: Double
, pressure :: Int
, humidity :: Int
, temp_min :: Double
, temp_max :: Double
} deriving (Show,Generic)
instance FromJSON MainWeatherInfo
instance FromJSON Main
main :: IO ()
main = do
jsonString <- B.readFile "/home/ashot/test.json"
let result = decodeStrict $ jsonString :: Maybe MainWeatherInfo
case result of
Nothing -> print "error"
Just result -> print result
和JSON文件:
{
"coord": {
"lon": 44.51,
"lat": 40.18
},
"weather": [{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}],
"base": "stations",
"main": {
"temp": 299.15,
"pressure": 1008,
"humidity": 34,
"temp_min": 299.15,
"temp_max": 299.15
},
"visibility": 10000,
"wind": {
"speed": 1.5,
"deg": 190
},
"rain": {
"3h": 0.195
},
"clouds": {
"all": 8
},
"dt": 1528615800,
"sys": {
"type": 1,
"message": 0.0036,
"country": "AM",
"sunrise": 1528594331,
"sunset": 1528648255
},
"id": 616052,
"name": "Yerevan",
"cod": 200
}
我需要 Extract "main" info.And 我在启动时输入 MainWeatherInfo.This 代码是 compiled.But 我得到一个 "error"。我该如何解决这个问题并获得正常信息?
问题是 aeson 正在文件中寻找一个名为 mainInfo 的字段,但这样的字段不存在。
最初我认为我们可以通过将 MainWeatherInfo 的类型定义中的 mainInfo 更改为 main[ 来解决问题=43=]。这是行不通的,因为 已经存在同名函数!
aeson 提供了解决此问题的方法,您可以在 泛型:自定义字段名称[=43] 部分下找到 this helpful tutorial 中的说明=].
更具体地说,如果您使用 turorial 的方法,您最终会得到一个 Main.hs,如下所示:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
module Main where
import GHC.Generics
import Data.Aeson
import Data.Aeson.Types
import qualified Data.ByteString as L
data MainWeatherInfo = MainWeatherInfo
{ mainInfo :: Main
} deriving ( Show
, Generic
)
instance ToJSON MainWeatherInfo where
toJSON = genericToJSON defaultOptions { fieldLabelModifier = take 4 }
instance FromJSON MainWeatherInfo where
parseJSON = genericParseJSON defaultOptions { fieldLabelModifier = take 4 }
data Main = Main
{ temp :: Double
, pressure :: Int
, humidity :: Int
, temp_min :: Double
, temp_max :: Double
} deriving ( Show
, Generic
, ToJSON
, FromJSON
)
main :: IO ()
main = do
contents <- L.readFile "notes/test.json"
let result = (decodeStrict contents) :: Maybe MainWeatherInfo
case result of
Nothing -> putStrLn "error"
Just v -> putStrLn $ show v
我在我的电脑上试了一下,效果很好。
更新: 我一直非常愚蠢并认为解决方案需要仅限于一个模块。有一个看起来像 Main.hs 的 much 可能会更好:
module Main where
import Data.Aeson
import qualified Data.ByteString as L
import qualified Weather as W
main :: IO ()
main = do
contents <- L.readFile "notes/test.json"
let result = (decodeStrict contents) :: Maybe W.MainWeatherInfo
case result of
Nothing -> putStrLn "error"
Just v -> putStrLn $ show v
还有另一个模块 Weather.hs 即:
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
module Weather where
import GHC.Generics
import Data.Aeson
import Data.Aeson.Types
data MainWeatherInfo = MainWeatherInfo
{ main :: Main
} deriving ( Show
, Generic
, ToJSON
, FromJSON
)
data Main = Main
{ temp :: Double
, pressure :: Int
, humidity :: Int
, temp_min :: Double
, temp_max :: Double
} deriving ( Show
, Generic
, ToJSON
, FromJSON
)
我测试了它只是为了确定,它确实有效。
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
module Main where
import Data.Aeson
import Data.Aeson.Encode.Pretty
import Data.Aeson.Types
import Data.ByteString as B
import qualified Data.ByteString.Lazy.Char8 as LC
import GHC.Generics
import Data.Text
data MainWeatherInfo = MainWeatherInfo
{ mainInfo :: Main
} deriving (Show,Generic)
data Main = Main
{ temp :: Double
, pressure :: Int
, humidity :: Int
, temp_min :: Double
, temp_max :: Double
} deriving (Show,Generic)
instance FromJSON MainWeatherInfo
instance FromJSON Main
main :: IO ()
main = do
jsonString <- B.readFile "/home/ashot/test.json"
let result = decodeStrict $ jsonString :: Maybe MainWeatherInfo
case result of
Nothing -> print "error"
Just result -> print result
和JSON文件:
{
"coord": {
"lon": 44.51,
"lat": 40.18
},
"weather": [{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}],
"base": "stations",
"main": {
"temp": 299.15,
"pressure": 1008,
"humidity": 34,
"temp_min": 299.15,
"temp_max": 299.15
},
"visibility": 10000,
"wind": {
"speed": 1.5,
"deg": 190
},
"rain": {
"3h": 0.195
},
"clouds": {
"all": 8
},
"dt": 1528615800,
"sys": {
"type": 1,
"message": 0.0036,
"country": "AM",
"sunrise": 1528594331,
"sunset": 1528648255
},
"id": 616052,
"name": "Yerevan",
"cod": 200
}
我需要 Extract "main" info.And 我在启动时输入 MainWeatherInfo.This 代码是 compiled.But 我得到一个 "error"。我该如何解决这个问题并获得正常信息?
问题是 aeson 正在文件中寻找一个名为 mainInfo 的字段,但这样的字段不存在。
最初我认为我们可以通过将 MainWeatherInfo 的类型定义中的 mainInfo 更改为 main[ 来解决问题=43=]。这是行不通的,因为 已经存在同名函数!
aeson 提供了解决此问题的方法,您可以在 泛型:自定义字段名称[=43] 部分下找到 this helpful tutorial 中的说明=].
更具体地说,如果您使用 turorial 的方法,您最终会得到一个 Main.hs,如下所示:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
module Main where
import GHC.Generics
import Data.Aeson
import Data.Aeson.Types
import qualified Data.ByteString as L
data MainWeatherInfo = MainWeatherInfo
{ mainInfo :: Main
} deriving ( Show
, Generic
)
instance ToJSON MainWeatherInfo where
toJSON = genericToJSON defaultOptions { fieldLabelModifier = take 4 }
instance FromJSON MainWeatherInfo where
parseJSON = genericParseJSON defaultOptions { fieldLabelModifier = take 4 }
data Main = Main
{ temp :: Double
, pressure :: Int
, humidity :: Int
, temp_min :: Double
, temp_max :: Double
} deriving ( Show
, Generic
, ToJSON
, FromJSON
)
main :: IO ()
main = do
contents <- L.readFile "notes/test.json"
let result = (decodeStrict contents) :: Maybe MainWeatherInfo
case result of
Nothing -> putStrLn "error"
Just v -> putStrLn $ show v
我在我的电脑上试了一下,效果很好。
更新: 我一直非常愚蠢并认为解决方案需要仅限于一个模块。有一个看起来像 Main.hs 的 much 可能会更好:
module Main where
import Data.Aeson
import qualified Data.ByteString as L
import qualified Weather as W
main :: IO ()
main = do
contents <- L.readFile "notes/test.json"
let result = (decodeStrict contents) :: Maybe W.MainWeatherInfo
case result of
Nothing -> putStrLn "error"
Just v -> putStrLn $ show v
还有另一个模块 Weather.hs 即:
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
module Weather where
import GHC.Generics
import Data.Aeson
import Data.Aeson.Types
data MainWeatherInfo = MainWeatherInfo
{ main :: Main
} deriving ( Show
, Generic
, ToJSON
, FromJSON
)
data Main = Main
{ temp :: Double
, pressure :: Int
, humidity :: Int
, temp_min :: Double
, temp_max :: Double
} deriving ( Show
, Generic
, ToJSON
, FromJSON
)
我测试了它只是为了确定,它确实有效。