I'm working on simple Haskell programme that fetches a JSON string from a server, parses it, and does something with the data. The specifics are not really pertinent for the moment, the trouble I'm having is with parsing the JSON that is returned.
我正在研究简单的Haskell程序,该程序从服务器获取JSON字符串,解析它,并对数据执行某些操作。目前的细节并不是真正相关,我遇到的麻烦是解析返回的JSON。
I get the JSON string back from the server as an IO String
type and can't seem to figure out how to parse that to a JSON object.
我从服务器返回JSON字符串作为IO String类型,似乎无法弄清楚如何将其解析为JSON对象。
Any help would be much appreciated :)
任何帮助将非常感激 :)
Here is my code thus far.
到目前为止,这是我的代码。
import Data.Aeson
import Network.HTTP
main = do
src <- openURL "http://www.reddit.com/user/chrissalij/about.json"
-- Json parsing code goes here
openURL url = getResponseBody =<< simpleHTTP (getRequest url)
Note: I'm using Data.Aeson
in the example as that is what seems to be recommended, however I'd be more than willing to use another library.
注意:我在示例中使用的是Data.Aeson,因为这似乎是推荐的,但是我更愿意使用另一个库。
Also any and all of this code can be changed. If getting the
此外,任何和所有代码都可以更改。如果得到了
1 个解决方案
#1
10
Data.Aeson
is designed to be used with Attoparsec, so it only gives you a Parser
that you must then use with Attoparsec. Also, Attoparsec prefers to work on ByteString
, so you have to alter the way the request is made slightly to get a ByteString
result instead of a String
.
Data.Aeson旨在与Attoparsec一起使用,因此它只为您提供一个Parser,然后您必须使用Attoparsec。此外,Attoparsec更喜欢使用ByteString,因此您必须稍微改变请求的方式以获取ByteString结果而不是String。
This seems to work:
这似乎有效:
import Data.Aeson
import Data.Attoparsec
import Data.ByteString
import Data.Maybe
import Network.HTTP
import Network.URI
main = do
src <- openURL "http://www.reddit.com/user/chrissalij/about.json"
print $ parse json src
openURL :: String -> IO ByteString
openURL url = getResponseBody =<< simpleHTTP (mkRequest GET (fromJust $ parseURI url))
Here I've just parsed the JSON as a plain Value
, but you'll probably want to create your own data type and write a FromJSON
instance for it to handle the conversion neatly.
在这里,我刚刚将JSON解析为普通值,但您可能希望创建自己的数据类型并为其编写FromJSON实例以便整齐地处理转换。
#1
10
Data.Aeson
is designed to be used with Attoparsec, so it only gives you a Parser
that you must then use with Attoparsec. Also, Attoparsec prefers to work on ByteString
, so you have to alter the way the request is made slightly to get a ByteString
result instead of a String
.
Data.Aeson旨在与Attoparsec一起使用,因此它只为您提供一个Parser,然后您必须使用Attoparsec。此外,Attoparsec更喜欢使用ByteString,因此您必须稍微改变请求的方式以获取ByteString结果而不是String。
This seems to work:
这似乎有效:
import Data.Aeson
import Data.Attoparsec
import Data.ByteString
import Data.Maybe
import Network.HTTP
import Network.URI
main = do
src <- openURL "http://www.reddit.com/user/chrissalij/about.json"
print $ parse json src
openURL :: String -> IO ByteString
openURL url = getResponseBody =<< simpleHTTP (mkRequest GET (fromJust $ parseURI url))
Here I've just parsed the JSON as a plain Value
, but you'll probably want to create your own data type and write a FromJSON
instance for it to handle the conversion neatly.
在这里,我刚刚将JSON解析为普通值,但您可能希望创建自己的数据类型并为其编写FromJSON实例以便整齐地处理转换。