提升 JsonResponse 反斜杠字符串

发布于 2024-12-16 19:44:57 字数 1790 浏览 1 评论 0原文

全部!

我正在尝试使用 Lift 和 Scala 创建 Rest Web 服务,该服务从具有映射缩减功能的 MOngoDB 获取结果。例如,这是我的服务功能:

val map ="""function map(){ 
 filteredDur= [];
 this."""+sendername+""".forEach(function (s){
  if (s.start > new ISODate(""""+ timestamp +""":00.00+02:00")){
    filteredDur.push(s);
  }
 });
 filteredDur.sort(function(a,b) {
   return a.start-b.start
 })
 emit(this._id, {After: filteredDur});
}"""

 val reduce = "function(key, values) { return values[0]; }"
 val mr = MapReduceCommand(mongoColl.getName(), map, reduce, MapReduceInlineOutput, None,  None, None, None)
 val result = mongoColl.mapReduce(mr)
 var temp = result.toString()
   //here some transformations of map_reduce result like getting read of map_reduce      //comments
 return JString(temp);

serve { //this are Lift's RestHelper methods
 case Req("sendungen" :: "sendungen" ::Nil, suffix,  GetRequest) => getAllSendungen()
}

我在 Lift 中使用 RestHelper。问题是我没有得到有效的 JSON :( 我得到的所有引号都带有反斜杠。结果:

{ \"desc\" : \"\" , \"duration\" : 30 , \"start\" : { \"$date\" : \"2010-07-13T09:30:00Z\"} , \"end\" : { \"$date\" : \"2010-07-13T10:00:00Z\"} 

我的客户端解析器无法解析此内容。所以我想:

{ "desc" : "" , "duration" : 30 , "start" : { "$date" : "2010-07-13T09:30:00Z"}

如何才能完成此工作?我知道我可以替换客户端中的斜杠应用程序,但我认为这不是很优雅。我尝试使用案例类,但这里得到了相同的案例类:

 case class Sendung(duration:Int, subtitle:String, desc:String,  image:String,       sender:String,end:java.util.Date, title:String, start:java.util.Date) {

  def toJSON (e : Sendung) : JObject = {
  import net.liftweb.json.JsonDSL._
  import net.liftweb.json.JsonAST._
   ("key" ->
   ("duration" -> JInt(duration))) ~
   ("subtitle" -> subtitle) ~
   ("desc" -> desc));
  }

 }

all!

I am trying to create Rest web service with Lift and Scala, which gets results from MOngoDB with map reduce functions. So for example this is my service function:

val map ="""function map(){ 
 filteredDur= [];
 this."""+sendername+""".forEach(function (s){
  if (s.start > new ISODate(""""+ timestamp +""":00.00+02:00")){
    filteredDur.push(s);
  }
 });
 filteredDur.sort(function(a,b) {
   return a.start-b.start
 })
 emit(this._id, {After: filteredDur});
}"""

 val reduce = "function(key, values) { return values[0]; }"
 val mr = MapReduceCommand(mongoColl.getName(), map, reduce, MapReduceInlineOutput, None,  None, None, None)
 val result = mongoColl.mapReduce(mr)
 var temp = result.toString()
   //here some transformations of map_reduce result like getting read of map_reduce      //comments
 return JString(temp);

serve { //this are Lift's RestHelper methods
 case Req("sendungen" :: "sendungen" ::Nil, suffix,  GetRequest) => getAllSendungen()
}

I am using RestHelper in Lift. the problem is I don't get a valid JSON :( I get all quotes backslashed. result:

{ \"desc\" : \"\" , \"duration\" : 30 , \"start\" : { \"$date\" : \"2010-07-13T09:30:00Z\"} , \"end\" : { \"$date\" : \"2010-07-13T10:00:00Z\"} 

and my client parser doesn't parse this. so I want:

{ "desc" : "" , "duration" : 30 , "start" : { "$date" : "2010-07-13T09:30:00Z"}

How can I make this work? I know I can replace slashes in client application, but i don't think this is very elegant. I tried using case classes, but got the same. here is case class:

 case class Sendung(duration:Int, subtitle:String, desc:String,  image:String,       sender:String,end:java.util.Date, title:String, start:java.util.Date) {

  def toJSON (e : Sendung) : JObject = {
  import net.liftweb.json.JsonDSL._
  import net.liftweb.json.JsonAST._
   ("key" ->
   ("duration" -> JInt(duration))) ~
   ("subtitle" -> subtitle) ~
   ("desc" -> desc));
  }

 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

违心° 2024-12-23 19:44:57

已解决:
JsonResponse.apply(net.liftweb.json.JsonParser.parse(result_string))

返回带非反斜杠的 LiftResponse。不幸的是,JString 没有三引号选项。这就是为什么它不起作用

Solved:
JsonResponse.apply(net.liftweb.json.JsonParser.parse(result_string))

returns LiftResponse with non-backslashed. unfortunately, JString doesn't have an option of triple quotes. that is why it didn't work

木森分化 2024-12-23 19:44:57

我们在项目中遇到了类似的问题——我们需要通过 JsonResponse 对象返回 JSON。实际上就像这样做一样简单:

val json = ("userId" -> userId) ~ ("userToken" -> userToken)
JsonResponse(json)

这样您就可以在响应中得到漂亮的、未转义的 JSON。您的客户端解析器应该能够做到这一点。

We were running into a similar problem on our project -- we needed to return JSON through a JsonResponse object. It was actually as simple as doing:

val json = ("userId" -> userId) ~ ("userToken" -> userToken)
JsonResponse(json)

With that you get nice, unescaped JSON in the response. Your client-side parser should be able to work that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文