如何创建包含html代码的json对象

发布于 2024-11-03 07:30:58 字数 328 浏览 1 评论 0原文

我有一个情况,我必须通过 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 技术交流群。

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

发布评论

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

评论(5

森林很绿却致人迷途 2024-11-10 07:30:58

我不确定您如何构建 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() and json_decode() you shouldn't have any problems with HTML characters, they'll be escaped properly.

温柔戏命师 2024-11-10 07:30:58

不确定是否我误读了问题,但要将 json 从服务器传递到客户端,请使用json_encode ()

http://php.net/manual/en/function .json-encode.php

这应该适合您并处理所有转义。

<?php
header('Content-type: application/json');
$json = array(
    'html'    =>    '<h1 style="color:#0F0">Hello World!</h1>'
);
echo json_encode($json);
exit;

嗯,刚刚从阅读文档中学到了这个花絮:

json_encode() 不适用于
UTF-8 以外的字符集

我不明白为什么你需要将 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.

<?php
header('Content-type: application/json');
$json = array(
    'html'    =>    '<h1 style="color:#0F0">Hello World!</h1>'
);
echo json_encode($json);
exit;

Hmm, just learned this tidbit from reading the docs:

json_encode() won't work with
character sets other than UTF-8

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?

蹲墙角沉默 2024-11-10 07:30:58

也许你可以使用 url 编码。

Perhaps you could use url encoding.

最美的太阳 2024-11-10 07:30:58

您可以对 html 进行编码。一些html编码和解码可以在这里找到:
http://code .google.com/p/jsool/source/browse/jsool-site/js/util/Encoder.js?r=176

你只需要 html.encode 和 html.decode 函数,这样你就可以做到这样

function html_encode(string){
    return string.replace(/./g,function(chr, index){
            return chr.match(/[\w\d]/) ? chr : "&#"+chr.charCodeAt(0)+";" ;
    });
},
function html_decode(string){
    return string.replace(/&#[0-9]+;/g,function(text,index){
            return String.fromCharCode(text.match(/[0-9]+/)[0]);
    });
}

你就可以

var html = html_encode(myhtml);
{"html": html}

使用 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

function html_encode(string){
    return string.replace(/./g,function(chr, index){
            return chr.match(/[\w\d]/) ? chr : "&#"+chr.charCodeAt(0)+";" ;
    });
},
function html_decode(string){
    return string.replace(/&#[0-9]+;/g,function(text,index){
            return String.fromCharCode(text.match(/[0-9]+/)[0]);
    });
}

That way you can do

var html = html_encode(myhtml);
{"html": html}

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.

待天淡蓝洁白时 2024-11-10 07:30:58

如果您使用 jQuery,这个 js 库可能会有所帮助。 http://www .rahulsingla.com/blog/2010/05/jquery-encode-decode-任意-objects-to-and-from-json

在发送请求之前在您的javascript中执行以下操作:

var thing = {plugin: 'jquery-json', other: "blah-blah"};
var encoded = $.JSON.encode(thing);

然后将请求发送到 php 后,执行以下操作进行解码:

var thing = json_decode($_POST['thing']);

您可以在此处

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:

var thing = {plugin: 'jquery-json', other: "blah-blah"};
var encoded = $.JSON.encode(thing);

Then after you send the request to php, do this to decode:

var thing = json_decode($_POST['thing']);

You can read more on this here

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