Parsing JSON data from a URL in Haskell using Aeson -
i'm trying create web app in haskell takes json input url.
{-# language overloadedstrings, derivegeneric #-} import data.aeson q import data.text import control.applicative import control.monad import qualified data.bytestring.lazy b import network.http.conduit (simplehttp) import ghc.generics --import data.datetime data temperatures = temperatures { date :: string , temperature :: int } deriving (show, generic) instance fromjson temperatures instance tojson temperatures jsonurl :: string jsonurl = "http://www.phoric.eu/temperature" getjson :: io b.bytestring getjson = simplehttp jsonurl main :: io () main = d <- (eitherdecode <$> getjson) :: io (either string temperatures) case d of left e -> putstrln e right stuff -> print stuff
however, i'm getting error @ runtime:
bradley$ runhaskell test.hs key "date" not found
the json below @ url in code.
{"temperatures":[ {"date":"2015-02-28t20:16:12+00:00", "temperature":0}, {"date":"2015-01-01t21:46:55+00:00", "temperature":2}, {"date":"2015-04-08t21:46:53+00:00", "temperature":3}, {"date":"2015-04-09t21:46:01+00:00", "temperature":4}, {"date":"2015-04-10t21:46:40+00:00", "temperature":5}, {"date":"2015-04-11t21:46:36+00:00", "temperature":6}, {"date":"2015-04-12t20:36:25+00:00", "temperature":7} ]}
i have no idea why not recognising keys when they're present in json object, ideas?
in current form, code can parse single line of form {"date":"2015-04-12t20:36:25+00:00", "temperature":7}
: tries parse input temperatures
, can't find date
key root object because has temperatures
key.
the problem temperatures
datatype not match input. here should match:
data datapoint = datapoint { date :: string , temperature :: int } deriving (show, generic) data temperatures = temperatures { temperatures :: [datapoint] } deriving (show, generic)
Comments
Post a Comment