Prelude.read: no parse && Couldn't match expected type ‘Double’ with actual type ‘Text’ In the first argument of ‘WeatherValues’, namely ‘tempMin’

如何解决? Couldn't match expected type ‘Double’ with actual type ‘Text’ 我不能在双精度浮点数的位置使用文本。而这是一个响应

响应

{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"
点赞
用户791604
用户791604

下面这三个代码片段是不可调和的:

data WeatherValues = WeatherValues
           { temp_min :: Double
           }

let tempMin = getTempMin info
in WeatherValues tempMin

getTempMin :: Object -> Text

可能的解决方案是调整getTempMin函数,让它返回一个Maybe Double(可以通过某种方式解析提取的Text),并且调整extractValues函数以返回Maybe WeatherValues

2018-05-16 11:47:42