gpt4 book ai didi

json - Gson转换为JSON时再次转义转义序列-Android

转载 作者:行者123 更新时间:2023-12-02 13:43:18 25 4
gpt4 key购买 nike

我正在尝试将html字符串发送到服务器。
我有的HTML:

val htmlInput = "<div>ansi nmasbnmbsanbdmnsbmndbsanbdnbsand<br></div><div><span class=\"size\" style=\"font-size: 16px\">ggsgjfgjfdgfgdg</span><br></div><div><br></div><div><span class=\"size\" style=\"font-size: 16px\">fgsdgfd</span><br></div><div><br></div><div><br></div><div><ul><li>fgsfgdf<br></li></ul><div><br></div><div><ol><li>fgdsg<br></li><li>sdfgsd<br></li><li>sfgs<br></li><li>sdfg<br></li><li>gfdsffgf<br></li></ol><div><br></div><blockquote style=\"background-color: rgb(245, 245, 245); padding: 7px; border: 1px solid rgb(204, 204, 204);\"><div><div><span class=\"colour\" style=\"color:rgba(57, 229, 0, 2.6)\"><span class=\"highlight\" style=\"background-color:rgba(255, 64, 0, 2.6)\">jfkdsfsdfsfsdfs</span></span><br></div></div></blockquote></div><div><br></div><div>ajjfajfjajfjf<br></div><div><br></div><table border=\"1\" cellpadding=\"2\" cellspacing=\"2\" style=\"width: 100%;\"><tbody><tr><td>afafsadfasfdsa</td><td>ddadasdadadasd</td></tr><tr><td>adadasdasd</td><td>asdadadasdasda</td></tr></tbody></table><div><br></div><div><br></div></div><div><br></div>"

现在我正在尝试使用gson构造输入json
val inputData = mapOf("input" to htmlInput)
val gson = GsonBuilder().disableHtmlEscaping().create()
val inputDataString = gson.toJson(inputData)

现在,从上面的代码获得的inputDataString就像,
{ "input" : "<div>ansi nmasbnmbsanbdmnsbmndbsanbdnbsand<br><\/div><div><span class=\\\"size\\\" style=\\\"font-size: 16px\\\">ggsgjfgjfdgfgdg<\/span><br><\/div><div><br><\/div><div><span class=\\\"size\\\" style=\\\"font-size: 16px\\\">fgsdgfd<\/span><br><\/div><div><br><\/div><div><br><\/div><div><ul><li>fgsfgdf<br><\/li><\/ul><div><br><\/div><div><ol><li>fgdsg<br><\/li><li>sdfgsd<br><\/li><li>sfgs<br><\/li><li>sdfg<br><\/li><li>gfdsffgf<br><\/li><\/ol><div><br><\/div><blockquote style=\\\"background-color: rgb(245, 245, 245); padding: 7px; border: 1px solid rgb(204, 204, 204);\\\"><div><div><span class=\\\"colour\\\" style=\\\"color:rgba(57, 229, 0, 2.6)\\\"><span class=\\\"highlight\\\" style=\\\"background-color:rgba(255, 64, 0, 2.6)\\\">jfkdsfsdfsfsdfs<\/span><\/span><br><\/div><\/div><\/blockquote><\/div><div><br><\/div><div>ajjfajfjajfjf<br><\/div><div><br><\/div><table border=\\\"1\\\" cellpadding=\\\"2\\\" cellspacing=\\\"2\\\" style=\\\"width: 100%;\\\"><tbody><tr><td>afafsadfasfdsa<\/td><td>ddadasdadadasd<\/td><\/tr><tr><td>adadasdasd<\/td><td>asdadadasdasda<\/td><\/tr><\/tbody><\/table><div><br><\/div><div><br><\/div><\/div><div><br><\/div>" }

我不知道为什么Gson进一步转义了正确转义的字符串。建议我为使上述代码正常工作必须进行的更改。

提前致谢。

最佳答案

好吧,一切都很好,如果我们将报价单保留为",JSON格式将被破坏,例如:

{
"a" : ""b"
}

但是使用反斜杠保护(转义字符), "\""可以:
{
"a": "\"b"
}

I'm trying to send a html string to server.



如果请求中包含 "content-type", "application/json",则服务器端应进行处理。无事可做

关于 disableHtmlEscaping:

By default, Gson escapes HTML characters such as < > etc. Use this option to configure Gson to pass-through HTML characters as is.

关于json - Gson转换为JSON时再次转义转义序列-Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61406758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com