【发布时间】:2013-01-25 18:02:01
【问题描述】:
这是我的控制器方法:
public function sendjsonAction()
{
$message = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Message')
->findAll();
$serializer = new Serializer(array(new GetSetMethodNormalizer()), array('message' => new
JsonEncoder()));
$message = $serializer->serialize($message, 'json');
return new JsonResponse($message);
}
这是我的路由器:
acme_store_sendjson:
pattern: /sendjson/
defaults: { _controller: AcmeStoreBundle:Default:sendjson}
这就是我去 /sendjson/ 时得到的结果:
"[{\u0022id\u0022:1,\u0022iam\u0022:1,\u0022youare\u0022:2,\u0022lat\u0022:50.8275853,\u0022lng\u0022:4.3809764,\u0022date\u0022:{\u0022lastErrors\u0022:{\u0022warning_count\u0022:0,\u0022warnings\u0022:[],\u0022error_count\u0022:0,\u0022errors\u0022:[]},\u0022timezone\u0022:{\u0022name\u0022:\u0022UTC\u0022,\u0022location\u0022:{\u0022country_code\u0022:\u0022??
(类似的情况)
我尝试使用以下内容进行 ajax 调用(使用 jQuery):
$.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');
});
我得到一个
Uncaught TypeError: Cannot use 'in' operator to search for '1549' in [{"id":1,...
当我更改 Symfony2 的响应类型时,我得到一个列表
[对象] [对象] [对象] [对象] [对象] [对象] ...
我做错了什么?我应该解析答案以将 \u0022 转换为 " 还是我的响应从一开始就有错误?
编辑
我还尝试将控制器更改为:
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);
}
这次我得到了 VALID JSON,(根据 Jsonlint)但标题不是 application/json...(有意义,因为我发送的是响应而不是 JsonResponse...)(但这就是我正在尝试的避免因为 JsonResponse 似乎正在改变添加奇怪的字符)
[{"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!"}]
【问题讨论】:
-
你的 php 看起来不错,\u0022 是一个引号。请调试您的javascript,错误应该在您的js代码中。
-
js 是正确的,直接来自 jquery.com,当我在数组中手动内联数据并将它们作为响应发送时它可以工作,所以问题是 json ......我不应该有引号作为 \u0022 字符..(没有 js 那里 - 那是 Symfony 的直接页面)
-
尝试新的响应($message)。你得到了什么?
-
[{"id":1,"iam":1,"youare":2,"lat":50.8275853,"lng":4.3809764,"date":{"lastErrors":{ "warning_count":0,"warnings":[],"error_count":0 引号是正确的,但有些条目我不知道它们来自哪里,“lastErrors,warning_count,warnings,那些东西是不在我的数据库中。完全没有。getjson 调用现在给出:[object Object] [object Object] etc etc..
-
很可能有来自序列化过程的消息。 json响应其实是一个字符串,所以在使用前尝试解析成对象stackoverflow.com/questions/45015/…