[英]JSON_ENCODE tries to escape double
I am using this controller to fetch data from my database and then send them back as valid JSON data but my http response although has valid JSON, it's text/html instead of application/json so getJSON doesn't work. 我正在使用此控制器从数据库中获取数据,然后将其作为有效的JSON数据发送回去,但我的http响应虽然具有有效的JSON,但它是text / html而不是application / json,因此getJSON不起作用。 (unless getJSON is supposed to work anyway?)
(除非getJSON应该仍然可以工作?)
public function sendjsonAction()
{
$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new GetSetMethodNormalizer());
$serializer = new Serializer($normalizers, $encoders);
$message = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Message')
->findAll();
$serializer = $serializer->serialize($message, 'json');
return new Response($serializer);
}
here's what I get when I visit the json url: 这是我访问json网址时得到的信息:
[{"id":1,"iam":1,"youare":2,"lat":50.8275853,"lng":4.3809764,"msgbody":"I saw you over there what's up!"},{"id":2,"iam":1,"youare":2,"lat":50.8275853,"lng":4.3809764,"msgbody":"I saw you over there what's up!"},{"id":3,"iam":1,"youare":2,"lat":50.8275853,"lng":4.3809764,"msgbody":"I saw you over there what's up!"},{"id":4,"iam":1,"youare":2,"lat":50.8275853,"lng":4.3809764,"msgbody":"I saw you over there what's up!"},{"id":5,"iam":1,"youare":2,"lat":50.8275853,"lng":4.3809764,"msgbody":"I saw you over there what's up!"},{"id":6,"iam":1,"youare":2,"lat":50.8275853,"lng":4.3809764,"msgbody":"I saw you over there what's up!"}] [{“ id”:1,“ iam”:1,“ youare”:2,“ lat”:50.8275853,“ lng”:4.3809764,“ msgbody”:“我在那边看到你了!”},{“ id“:2,” iam“:1,” youare“:2,” lat“:50.8275853,” lng“:4.3809764,” msgbody“:”我在那边看到你了!“},{” id“: 3,“ iam”:1,“ youare”:2,“ lat”:50.8275853,“ lng”:4.3809764,“ msgbody”:“我在那边看到你了!”},{“ id”:4,“ iam“:1,” youare“:2,” lat“:50.8275853,” lng“:4.3809764,” msgbody“:”我在那边看到你了!“,{” id“:5,” iam“: 1,“ youare”:2,“ lat”:50.8275853,“ lng”:4.3809764,“ msgbody”:“我在那边看到你了!”},{“ id”:6,“ iam”:1,“ youare“:2,” lat“:50.8275853,” lng“:4.3809764,” msgbody“:”我在那边看到你了!“}]
So I have another page, called "showall" which has the following Javascript: 因此,我有另一个页面,名为“ showall”,其中包含以下Javascript:
$.getJSON('/app_dev.php/sendjson', function(data) {
var items = [];
$.each(data, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
});
And the result of that JS is this html: JS的结果就是这个html:
<ul class="my-new-list">
<li id="0">[object Object]</li>
<li id="1">[object Object]</li>
<li id="2">[object Object]</li>
<li id="3">[object Object]</li>
<li id="4">[object Object]</li>
<li id="5">[object Object]</li>
</ul>
If I attempt to use json_encode, then things get even messier.. Here's the answer I get: 如果我尝试使用json_encode,那么事情将变得更加混乱。这是我得到的答案:
"[{\"id\":1,\"iam\":1,\"youare\":2,\"lat\":50.8275853,\"lng\":4.3809764,\"msgbody\":\"I saw you over there what's up!\"},{\"id\":2,\"iam\":1,\"youare\":2,\"lat\":50.8275853,\"lng\":4.3809764,\"msgbody\":\"I saw you over there what's up!\"},{\"id\":3,\"iam\":1,\"youare\":2,\"lat\":50.8275853,\"lng\":4.3809764,\"msgbody\":\"I saw you over there what's up!\"},{\"id\":4,\"iam\":1,\"youare\":2,\"lat\":50.8275853,\"lng\":4.3809764,\"msgbody\":\"I saw you over there what's up!\"},{\"id\":5,\"iam\":1,\"youare\":2,\"lat\":50.8275853,\"lng\":4.3809764,\"msgbody\":\"I saw you over there what's up!\"},{\"id\":6,\"iam\":1,\"youare\":2,\"lat\":50.8275853,\"lng\":4.3809764,\"msgbody\":\"I saw you over there what's up!\"}]"
escaped double quotes... ! 转义的双引号...! Should I add an option to json_encode so that it doesn't escape these double quotes?
我应该在json_encode中添加一个选项,以便它不会转义这些双引号吗?
如果您依赖Content-Type
,请使用header()
设置适当的内容:
header('Content-type: application/json');
Change: 更改:
items.push('<li id="' + key + '">' + val + '</li>');
to: 至:
items.push('<li id="' + key + '">' + val.msgbody + '</li>');
You don't need to set the Content-type when using $.getJSON
-- it assumes the output is JSON. 使用
$.getJSON
时不需要设置Content-type -它假定输出为JSON。 You need it if you use more generic methods like $.get
, $.post
, or $.ajax
. 如果使用
$.get
, $.post
或$.ajax
等更通用的方法,则需要它。 They all allow you to specify a dataType: 'json'
option to tell it that the output is JSON as well, but if you leave out the option they look at the Content-type
to decide how to parse it. 它们全都允许您指定
dataType: 'json'
选项,以告知其输出也是JSON,但是如果省略该选项,则它们将查看Content-type
来决定如何解析它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.