Prelude.read:没有解析&&无法将预期类型'Double'与实际类型'Text'匹配在'WeatherValues'的第一个参数中,即'tempMin'

时间:2018-05-16 11:13:20

标签: haskell aeson

我该如何解决? Couldn't match expected type ‘Double’ with actual type ‘Text’ 我无法使用文本代替double。这是一个回复

回复

{responseStatus = Status {statusCode = 200, statusMessage = "OK"}, 
responseVersion = HTTP/1.1, responseHeaders = [("Server","openresty"),
("Date","Wed, 16 May 2018 11:12:26 GMT"),("Content-Type","application/json; 
charset=utf-8"),("Content-Length","446"),("Connection","keep-alive"),("X-Cache-
Key","/data/2.5/weather?q=yerevan,am"),("Access-Control-Allow-Origin","*"),
("Access-Control-Allow-Credentials","true"),("Access-Control-Allow-
Methods","GET, POST")], responseBody = "{\"coord\":
{\"lon\":44.51,\"lat\":40.18},\"weather\":
[{\"id\":801,\"main\":\"Clouds\",\"description\":\"few 
clouds\",\"icon\":\"02d\"}],\"base\":\"stations\",\"main\":
{\"temp\":298.15,\"pressure\":1019,\"humidity\":23,\"temp_min\":298.15,\"temp_ma
x\":298.15},\"visibility\":10000,\"wind\":
{\"speed\":1.5,\"deg\":220},\"clouds\":{\"all\":20},\"dt\":1526466600,\"sys\":
{\"type\":1,\"id\":7226,\"message\":0.0032,\"country\":\"AM\",\"sunrise\":152643
5114,\"sunset\":1526487120},\"id\":616052,\"name\":\"Yerevan\",\"cod\":200}", 
responseCookieJar = CJ {expose = []}, responseClose' = ResponseClose}

代码:

{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}    

module PrepareAnswer where

import           Control.Monad
import           Data.Maybe
import           GHC.Generics

import           Data.Aeson
import           Data.Aeson.Types
import qualified Data.ByteString          as BS
import qualified Data.ByteString.Lazy     as BSL
import           Data.Text
import           Network.HTTP.Client
import           Text.Read
import           Data.Text         
import           Text.JSON
import           AskWeather

data WeatherValues = WeatherValues
               { temp_min :: Double          
            -- , temp_max :: Text
            -- , pressure :: Text
            -- , speed     :: Text
               } deriving (Show) -- Здесь speed подразумевается как скорость ветра

prepareAnswer :: Response BSL.ByteString -> Text
prepareAnswer response = Data.Text.pack . show $ weatherValues
    where
      --finalPhrase = createFinalPrase preparedValues
    --  preparedValues = prepareValues weatherValues
        weatherValues = extractValues . responseBody $ response

extractValues :: BSL.ByteString -> WeatherValues
extractValues rawJSON =
    let result  = decode' rawJSON
    in case result of
        Nothing   -> error "Invalid JSON!"
        Just info -> 
                let  tempMin   = getTempMin info
          --       tempMax   = getTempMax   info
           --      pressInfo = getPressure  info
           --  windSpeed = getWindSpeed info
        in WeatherValues tempMin -- tempMax pressInfo windSpeed

getTempMin :: Object -> Text
getTempMin info =
    case parseMaybe extractTempMin info of 
        Nothing -> "Invalid JSON!"
        Just info -> info 
    where
        extractTempMin = \info -> info .: "main"
                         >>=
                         \mainInfo -> mainInfo .: "temp_min"

1 个答案:

答案 0 :(得分:2)

您的代码中的这三个代码段是不可调和的:

data WeatherValues = WeatherValues
           { temp_min :: Double
           }

let tempMin = getTempMin info
in WeatherValues tempMin

getTempMin :: Object -> Text

正确的解决方案可能是调整getTempMin以返回Maybe Double(通过解析您以某种方式提取的Text),并调整extractValues返回Maybe WeatherValues