为什么Groovy JSONBuilder在网址中跳过斜杠? [英] Why does the Groovy JSONBuilder escape slashes in URLs?

查看:100
本文介绍了为什么Groovy JSONBuilder在网址中跳过斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要将JSON POST到一个URL的Groovy脚本。我注意到一个问题是,我的JSON中包含'/'的所有元素都被JSON Builder更改为'\ /'。有没有办法阻止它?



这是使用Groovy 1.8。这里有一个简单的例子和​​它的输出:

$ pre $ def $ json = new JsonBuilder()
json.reply {
结果'http://google.ie/testing'
}

println json.toString()

输出 - > {reply:{result:http:\ / \ / google.ie\ / testing}}

谢谢

解决方案

刚刚看了一下, groovy.json。 JsonOuput.toJson(string)将正斜杠编码为'\\ /'



您可以使用 toPrettyString ,但它不会这样做:

  def json = new groovy.json.JsonBuilder()

json.reply {
结果'http://google.ie/testing'
}

assert json.toPrettyString()=='''{
reply:{
result:http://google.ie/testing
}
}'''


I am writing a Groovy script that needs to POST JSON to a URL. I have noticed a problem were all elements in my JSON that contains a '/' are changed to '\/' by the JSON Builder. Is there a way to stop this?

This is using Groovy 1.8. Here is a simple example and its output:

def json = new JsonBuilder()
    json.reply {
    result 'http://google.ie/testing'
}

println json.toString()

Output -> {"reply":{"result":"http:\/\/google.ie\/testing"}}

Thanks

解决方案

Just had a look, and groovy.json.JsonOuput.toJson(string) encodes forward slash as '\\/'.

You can use toPrettyString and it doesn't do it however:

def json = new groovy.json.JsonBuilder()

json.reply {
  result 'http://google.ie/testing'
}

assert json.toPrettyString() == '''{
    "reply": {
        "result": "http://google.ie/testing"
    }
}'''

这篇关于为什么Groovy JSONBuilder在网址中跳过斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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