Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 如何从数据中获取键值_Haskell - Fatal编程技术网

Haskell 如何从数据中获取键值

Haskell 如何从数据中获取键值,haskell,Haskell,我是Haskell的新手,希望为用户解析JSON 有这样的代码: import Data.Aeson as Q import Data.Text import Control.Applicative import Control.Monad import qualified Data.ByteString.Lazy as B import Network.HTTP.Conduit (simpleHttp) import GHC.Generics data DataPoint = DataPoi

我是Haskell的新手,希望为用户解析JSON

有这样的代码:

import Data.Aeson as Q
import Data.Text
import Control.Applicative
import Control.Monad
import qualified Data.ByteString.Lazy as B
import Network.HTTP.Conduit (simpleHttp)
import GHC.Generics

data DataPoint = DataPoint { id :: Int
                           , description :: String
                           , icon :: String
                           } deriving (Show, Generic)

data Temperatures = Temperatures { weather :: [DataPoint]
                                 } deriving (Show, Generic)

instance FromJSON Temperatures
instance ToJSON Temperatures
instance FromJSON DataPoint
instance ToJSON DataPoint`

jsonURL :: String -> String
jsonURL q = "url here hidden"

getJSON :: String -> IO B.ByteString
getJSON town = simpleHttp (jsonURL town)

main :: IO ()
main = do
 putStrLn "Hello! Please insert your town >>> \n "
 town <- getLine
 putStrLn ("Your town is: " ++ town ++ "\n")
 d <- (eitherDecode <$> (getJSON town)) :: IO (Either String Temperatures)
 case d of
  Left e -> putStrLn "Error occured. Try again please"
  Right stuff -> putStrLn (fmap weather) $ stuff
导入数据。Aeson作为Q
导入数据.Text
导入控制
进口管制
将限定数据.ByteString.Lazy作为B导入
导入Network.HTTP.conductor(simpleHttp)
进口GHC.仿制药
数据点=数据点{id::Int
,description::String
,icon::String
}派生(显示,通用)
数据温度=温度{天气::[数据点]
}派生(显示,通用)
来自JSON的实例
实例温度
来自JSON数据点的实例
实例到JSON数据点`
jsonURL::字符串->字符串
jsonURL q=“此处隐藏的url”
getJSON::String->IO B.ByteString
getJSON town=simpleHttp(jsonURL town)
main::IO()
main=do
putStrLn“您好!请插入您的城镇>>\n”
城市公园(fmap天气)$stuff
想要显示id和描述,但映射方式不正确,是int吗? 可以这样存储数据吗?还是我应该这样

想要显示id和描述,但映射方式不正确,是int吗

是的,这不是正确的方法

实际上,您可以使用
weather
Temperatures
获取
数据点的列表,如下所示:

weather stuff
import Control.Monad (mapM_)
import Prelude hiding (id)

...
Right stuff -> mapM_ (\d->putStrLn (show (id d) ++ " " ++ (description d))) 
                     (weather stuff)
...
并使用
mapM
DataPoint
列表中打印
id
description
,如下所示:

weather stuff
import Control.Monad (mapM_)
import Prelude hiding (id)

...
Right stuff -> mapM_ (\d->putStrLn (show (id d) ++ " " ++ (description d))) 
                     (weather stuff)
...

请注意,它需要从
Prelude
中隐藏
id
函数。否则将出现不明确的错误。

您能澄清您的问题吗?我几乎不理解你的问题。我不明白你要打印什么——
(fmap weather)
肯定不是我也不明白这个问题,但最后一行应该是
正确的东西->putStrLn$weather东西