本文介绍了将JSON字符串解析成Haskell中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很努力地理解这一点(我对Haskell还是有点新鲜),但是我发现 Text.JSON 包的文档是一个有点混乱。基本上我有这样的数据记录类型: - $ / $>

  data Tweet = Tweet 
{
from_user :: String ,
to_user_id :: String,
profile_image_url :: String,
created_at :: String,
id_str :: String,
source :: String,
to_user_id_str :: String,
from_user_id_str :: String,
from_user_id :: String,
text :: String,
metadata :: String
}

我有一些JSON格式的推文符合这种类型的结构。我正在努力的事情是如何将上面的代码映射到从以下代码返回的内容中:

$ $ p $ decode tweet ::结果JSValue

转换为上述数据类型。我知道我应该创建一个实例JSON Tweet 的实例,但我不知道该从哪里去。



任何指针都将不胜感激,谢谢!

解决方案

您需要编写 showJSON readJSON 方法,用于将您的Haskell值构建为JSON格式。 JSON包将负责将原始字符串解析为。 包含字符串映射的 JSObject ,很可能。


  • 使用 show 来查看JSObject,看看这些字段是如何放置的。
  • 您可以在 JSObject 上使用 get_field 查找每个字段。

  • 您可以使用 fromJSString JSString 获取常规Haskell字符串。 。



一般而言,您需要类似的东西,

  { - #LANGUAGE RecordWildCards# - } 

import Text.JSON
import Text.JSON.Types

instance JSON Tweet where

readJSON(JSObject o)=返回$ Tweet {..}
其中from_user = grab ofrom_user
to_user_id = grab oto_user_id
profile_image_url = grab oproile_image_url
created_at = grab ocreated_at
id_str = grab oid_str
source = grab osource
to_user_id_str = grab oto_user_id_str
from_user_id_str = grab ofrom_user_id_str
from_user_id = grab ofrom_user_id
te xt = grab otext
metadata = grab ometadata


grab o s = case get_field o s of
Nothing - >错误Invalid field++ show s
Just(JSString s') - > fromJSString s'

请注意,我使用的是非常酷的通配符语言扩展。



没有JSON编码的例子,我没有更多的建议。






您可以通过实例找到用于JSON编码的示例实例



ul>
  • ,作为一个(低级)示例。


  • I'm struggling to understand this (I'm still a bit new to Haskell) but I'm finding the documentation for the Text.JSON package to be a little confusing. Basically I have this data record type: -

    data Tweet = Tweet
        {
            from_user :: String,
            to_user_id :: String,
            profile_image_url :: String,
            created_at :: String,
            id_str :: String,
            source :: String,
            to_user_id_str :: String,
            from_user_id_str :: String,
            from_user_id :: String,
            text :: String,
            metadata :: String
        }
    

    and I have some tweets in JSON format that conform to the structure of this type. The thing that I'm struggling with is how to map the above to what gets returned from the following code

    decode tweet :: Result JSValue
    

    into the above datatype. I understand that I'm supposed to create an instance of instance JSON Tweet but I don't know where to go from there.

    Any pointers would be greatly appreciated, thanks!

    解决方案

    You need to write a showJSON and readJSON method, for your type, that builds your Haskell values out of the JSON format. The JSON package will take care of parsing the raw string into a JSValue for you.

    Your tweet will be a JSObject containing a map of strings, most likely.

    • Use show to look at the JSObject, to see how the fields are laid out.
    • You can lookup each field using get_field on the JSObject.
    • You can use fromJSString to get a regular Haskell strings from a JSString.

    Broadly, you'll need something like,

    {-# LANGUAGE RecordWildCards #-}
    
    import Text.JSON
    import Text.JSON.Types
    
    instance JSON Tweet where
    
        readJSON (JSObject o) = return $ Tweet { .. }
                where from_user         = grab o "from_user"
                      to_user_id        = grab o "to_user_id"
                      profile_image_url = grab o "proile_image_url"
                      created_at        = grab o "created_at"
                      id_str            = grab o "id_str"
                      source            = grab o "source"
                      to_user_id_str    = grab o "to_user_id_str"
                      from_user_id_str  = grab o "from_user_id_str"
                      from_user_id      = grab o "from_user_id"
                      text              = grab o "text"
                      metadata          = grab o "metadata"
    
    
    grab o s = case get_field o s of
                    Nothing            -> error "Invalid field " ++ show s
                    Just (JSString s') -> fromJSString s'
    

    Note, I'm using the rather cool wild cards language extension.

    Without an example of the JSON encoding, there's not much more I can advise.


    Related

    You can find example instances for the JSON encoding via instances

    • in the source, forsimple types. Or in other packages that depend on json.
    • An instance for AUR messages is here, as a (low level) example.

    这篇关于将JSON字符串解析成Haskell中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-20 23:39