从耶索德处理程序返回JSON [英] Return JSON from yesod handler

查看:214
本文介绍了从耶索德处理程序返回JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写从耶索德的处理程序最简单的JSON响应,但有一些非常愚蠢的错误(显然)。我的处理程序code是这样的:

   -  HelloYesod /处理器/ Echo.hs
模块Handler.Echo哪里进口Data.Aeson(对象,(=))
进口资质Data.Aeson为J
进口Data.Text(包)
进口进口
进口Yesod.Core.Json(returnJson)getEchoR ::字符串 - >处理器RepJson
getEchoR theText = DO
  让JSON对象= $ [数据。=VAL]
  返回JSON

错误是这样的:

 处理器/ Echo.hs:12:10:
    无法比拟预期类型`RepJson'与实际类型`值
    在'归还',即'JSON'的第一个参数
    在'做'块的语句:返回JSON
    在EX pression:
      不要让{= JSON对象$ ...;
           返回JSON}
构建失败,暂停...


解决方案

我被这一抓过:你只需要改变你的类型签名,也将努力:

  getEchoR ::字符串 - >句柄值

我的理解是,整个众议员系统处于耶索德1.2 pcated德$ P $,所以处理器的现在返回HTML和价值,而不是RepHtml和RepJson。

希望这有助于!

I'm trying to write a simplest JSON response from Yesod's handler, but have some really stupid error (apparently). My handler code is this:

-- HelloYesod/Handler/Echo.hs
module Handler.Echo where

import           Data.Aeson      (object, (.=))
import qualified Data.Aeson      as J
import           Data.Text       (pack)
import           Import
import           Yesod.Core.Json (returnJson)

getEchoR :: String -> Handler RepJson
getEchoR theText = do
  let json = object $ ["data" .= "val"]
  return json

Error is this:

Handler/Echo.hs:12:10:
    Couldn't match expected type `RepJson' with actual type `Value'
    In the first argument of `return', namely `json'
    In a stmt of a 'do' block: return json
    In the expression:
      do { let json = object $ ...;
           return json }
Build failure, pausing...

解决方案

I got caught by this one too: you just have to change your type signature and it will work:

getEchoR :: String -> Handler Value

My understanding is that the whole Rep system is deprecated in Yesod 1.2, so Handler's now return Html and Value rather than RepHtml and RepJson.

Hope this helps!

这篇关于从耶索德处理程序返回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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