解析 JSON 帖子 [英] Parsing a JSON post

查看:40
本文介绍了解析 JSON 帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一段代码:

data Friend = Friend
              { friend_name :: Text
              , friend_inTwitter :: Bool
              , friend_twitterName :: Maybe Text
              }
$(deriveJSON (drop 6) ''Friend)

这段 JSON 正在发送到处理程序,我很难获得它.我尝试了不同的方法,但让我将其中之一放在这里以生成建议:

This piece of JSON is being posted to a handler, and I'm having a difficult time getting it. I've tried different things, but let me just put one of them here to generate suggestions:

postTestR :: Handler RepPlain
postTestR = do
value <- parseJsonBody_
return $ RepPlain $ friend_name value

这不起作用,我可以看到类型不匹配,但我不确定用什么替换它.我还想了解如何解析以 JSON 格式发布的好友列表.

That doesn't work, and I can see that the types don't match, but I'm not sure what to replace it with. I would also like to see how I could parse a list friends that gets posted as JSON.

谢谢!

推荐答案

好吧,事实证明,我需要在其中添加一个toContent"调用以将文本转换为内容.这是一段有效的代码:

Well, as it turn out, I needed to add a "toContent" call in there to convert from Text to Content. Here is a piece of code that works:

data Person = Person
              { person_firstName :: Text
              , person_lastName :: Text
              , person_fullName :: Text
              , person_friends :: [Friend]
              }

data Friend = Friend
              { friend_name :: Text
              , friend_inTwitter :: Bool
              , friend_twitterName :: Text
              }


$(deriveJSON (drop 7) ''Person)              
$(deriveJSON (drop 7) ''Friend)   


postKnockoutR :: Handler RepPlain
postKnockoutR = do
  value <- parseJsonBody_
  let (f:fs) = person_friends value
  return $ RepPlain $ toContent $ friend_name f

这篇关于解析 JSON 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆