[Haskell-beginners] Reading JSON using Text.JSON
Magnus Therning
magnus at therning.org
Sun May 15 23:20:19 CEST 2011
I wrote down some stuff from my first look at json, maybe it can
help you in getting further?
http://therning.org/magnus/archives/719
/M
On Sun, May 15, 2011 at 09:51:28PM +0200, Adrien Haxaire wrote:
> Hello,
>
> I have this JSON string that I have put in a text file, test.json:
>
> {"coordinates": [0.0, 1.0]}
>
> using GHCi, I can read it like this:
>
> > s <- readFile "test.json"
> > let r = decode s :: Result JSValue
> > r
> Ok (JSObject (JSONObject {fromJSObject = [("coordinates",JSArray
> [JSRational False (0 % 1),JSRational False (1 % 1)])]}))
>
> I want to affect this array of two Double to the type Coordinates
> being defined like this:
>
> type Coordinates = (Double, Double)
>
> Using this type I can create a Node:
>
> data Node = Node Coordinates Number
> deriving (Eq, Ord, Show)
>
> The problem is that I have no idea what to do with decoded JSValue.
> It seems I have to define an instance of JSON to read it. Does this
> mean using pattern matching to extract the values ?
>
> What do I do with the Result type ? Should I get rid of it like this:
>
> fromResult :: Result JSValue -> JSValue
> fromResult (Ok js) = js
> fromResult _ = JSNull
>
> and work with the raw JValue ?
>
> These questions might be trivial, but I know only imperative
> languages, which is why I like so much to learn Haskell and
> functional programming, lots to learn and discover :)
>
> Many thanks in advance,
> Adrien
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
--
Magnus Therning OpenPGP: 0xAB4DFBA4
email: magnus at therning.org jabber: magnus at therning.org
twitter: magthe http://therning.org/magnus
Perl is another example of filling a tiny, short-term need, and then
being a real problem in the longer term.
-- Alan Kay
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110515/e1fefb4f/attachment.pgp>
More information about the Beginners
mailing list