提升 JsonResponse 反斜杠字符串
全部!
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
已解决:
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
我们在项目中遇到了类似的问题——我们需要通过 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:
With that you get nice, unescaped JSON in the response. Your client-side parser should be able to work that.