如何创建包含html代码的json对象
我有一个情况,我必须通过 json 对象从客户端向服务器端发送 html 代码,但是 html 标记内的符号引起一些问题,例如“,/ ...等,这些问题会在 json 对象中创建错误我尝试了很多方法来构建json,但没有一个是正确的。我希望你清楚地理解我的情况。 这是我的 json 结构的一个例子:
{
"html":"the html code injected dynamical here"
}
正如我所说,我的主要问题是我遇到了由 html 符号引起的问题,我该如何解决它!请给我一个明确的例子来说明如何解决这个问题并将html代码成功解析为json对象。
i have a case where i have to send an html code from client side to server side through json object ,but there are some problems caused by symbols inside the html tag such as ",/ ...etc which create errors in the json object. I tried many ways to build the json but none of them were correct .I hop you understand my case clearly .
this is an example of my json structure:
{
"html":"the html code injected dynamical here"
}
as i said my main problem is that i am having problems caused by html symbols how can i solve it! please give me an explicit example on how to solve this problem and pars the html code successfully to json object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不确定您如何构建 JSON 对象,但如果您使用
json_encode()
和json_decode()
您应该不会遇到任何 HTML 问题字符,它们将被正确转义。I'm not sure about how you're building the JSON objects but if you're using
json_encode()
andjson_decode()
you shouldn't have any problems with HTML characters, they'll be escaped properly.不确定是否我误读了问题,但要将 json 从服务器传递到客户端,请使用
json_encode ()
http://php.net/manual/en/function .json-encode.php
这应该适合您并处理所有转义。
嗯,刚刚从阅读文档中学到了这个花絮:
我不明白为什么你需要将 json 发送到服务器,也许你可以与我们分享原因,或者有人可以在评论中启发我?
Not sure If I misread the question, but to pass json from server to client, use
json_encode()
http://php.net/manual/en/function.json-encode.php
This should work for you and take care of all the escaping.
Hmm, just learned this tidbit from reading the docs:
I can't see why you'd need to send json to the server, maybe you can share the reason with us or someone can enlighten me in the comments?
也许你可以使用 url 编码。
Perhaps you could use url encoding.
您可以对 html 进行编码。一些html编码和解码可以在这里找到:
http://code .google.com/p/jsool/source/browse/jsool-site/js/util/Encoder.js?r=176
你只需要 html.encode 和 html.decode 函数,这样你就可以做到这样
你就可以
使用 html_decode() 来编码和恢复值。
PS.:抱歉,我没有测试 html_encode 和decode 函数,但在 google 上很容易找到一些。
You can encode the html. Some html encoding and decode can be find here:
http://code.google.com/p/jsool/source/browse/jsool-site/js/util/Encoder.js?r=176
You just need the html.encode and html.decode functions, that way you can do something like
That way you can do
to encode and recover the values using html_decode().
PS.: Sry, i didnt tested out the html_encode and decode functions, but its easy to find some at google.
如果您使用 jQuery,这个 js 库可能会有所帮助。 http://www .rahulsingla.com/blog/2010/05/jquery-encode-decode-任意-objects-to-and-from-json
在发送请求之前在您的javascript中执行以下操作:
然后将请求发送到 php 后,执行以下操作进行解码:
您可以在此处
If you are using jQuery this js library might help. http://www.rahulsingla.com/blog/2010/05/jquery-encode-decode-arbitrary-objects-to-and-from-json
In your javascript before you send the request do this:
Then after you send the request to php, do this to decode:
You can read more on this here