Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析JSON haskell_Json_Haskell_Functional Programming_Request_Wreq - Fatal编程技术网

解析JSON haskell

解析JSON haskell,json,haskell,functional-programming,request,wreq,Json,Haskell,Functional Programming,Request,Wreq,我正在尝试访问请求中的内部对象。这是我的代码: {-# LANGUAGE OverloadedStrings #-} import Network.Wreq import Control.Lens import Data.Aeson import Data.Map as Map type Resp = Response (Map String Value) main = do r <- asJSON =<< get "https://bittrex.com/

我正在尝试访问请求中的内部
对象
。这是我的代码:

{-# LANGUAGE OverloadedStrings #-}


import Network.Wreq
import Control.Lens
import Data.Aeson

import Data.Map as Map

type Resp = Response (Map String Value)


main = do
    r <- asJSON =<< get "https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC" :: IO Resp
    let result = (r ^. responseBody)    
    print result
我试图访问“result”键的对象,然后访问其中的值。我不知道该怎么做,我把AESON和
^?
操作符弄乱了,
wreq
提供了这些操作符,但它们对我不起作用。

{-#语言重载字符串}
{-# LANGUAGE OverloadedStrings #-}

import Control.Lens
import Network.Wreq
import Data.Aeson.Lens

import Data.HashMap.Lazy (HashMap)
import Data.Text (Text)

main = do
  r <- asValue =<< get "https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC"
  let result =
        (r :: Response Value)
          -- get the Value out of the Response
          ^? responseBody
          -- The Value must be an Object, then unwrap the contained (HashMap Text Value)
          . _Object
          -- In the map {"message": ..., "result": ...}, get the "result".
          . ix "result"
          -- This must be an Object, unwrap the (HashMap Text Value)
          . _Object
  print (result :: HashMap Text Value)

-- How types line up:
--
-- responseBody . _Object . ix "result" . _Object
--         :: Traversal' (Response Value)                                  (HashMap Text Value)

--
-- responseBody :: Lens' (Response Value) Value
-- _Object      ::             Traversal' Value (HashMap Text Value)
-- ix "result"  ::                   Traversal' (HashMap Text Value) Value
-- _Object      ::                                        Traversal' Value (HashMap Text Value)
进口管制.镜头 导入网络.Wreq 导入Data.Aeson.Lens 导入Data.HashMap.Lazy(HashMap) 导入数据。文本(Text) main=do r
{-#语言重载字符串}
进口管制.镜头
导入网络.Wreq
导入Data.Aeson.Lens
导入Data.HashMap.Lazy(HashMap)
导入数据。文本(Text)
main=do
R
{-# LANGUAGE OverloadedStrings #-}

import Control.Lens
import Network.Wreq
import Data.Aeson.Lens

import Data.HashMap.Lazy (HashMap)
import Data.Text (Text)

main = do
  r <- asValue =<< get "https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC"
  let result =
        (r :: Response Value)
          -- get the Value out of the Response
          ^? responseBody
          -- The Value must be an Object, then unwrap the contained (HashMap Text Value)
          . _Object
          -- In the map {"message": ..., "result": ...}, get the "result".
          . ix "result"
          -- This must be an Object, unwrap the (HashMap Text Value)
          . _Object
  print (result :: HashMap Text Value)

-- How types line up:
--
-- responseBody . _Object . ix "result" . _Object
--         :: Traversal' (Response Value)                                  (HashMap Text Value)

--
-- responseBody :: Lens' (Response Value) Value
-- _Object      ::             Traversal' Value (HashMap Text Value)
-- ix "result"  ::                   Traversal' (HashMap Text Value) Value
-- _Object      ::                                        Traversal' Value (HashMap Text Value)