Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony2 JSON响应返回奇怪的UTF字符_Json_Symfony_Unicode - Fatal编程技术网

Symfony2 JSON响应返回奇怪的UTF字符

Symfony2 JSON响应返回奇怪的UTF字符,json,symfony,unicode,Json,Symfony,Unicode,以下是我的控制器方法: public function sendjsonAction() { $message = $this->getDoctrine() ->getRepository('AcmeStoreBundle:Message') ->findAll(); $serializer = new Serializer(array(new GetSetMethodNormalizer()), array('message' =&g

以下是我的控制器方法:

  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\u0022您是\u0022:2\u0022lat\u0022:50.8275853\u0022lng\u0022:4.3809764\u0022日期\u0022:{\u0022lastErrors\u0022:{\u0022警告计数\u0022:0\u0022警告\u0022:[],\u0022错误计数\u0022:0\u0022:0\u0022:[]时区\ u2222\u0022\u2222\u0022\u0022\u0022\u0022\u2222\u222\u222\u222\u222\u0022\u222\u222\u222\u222\u222\u222\u222\u222\u222\u{\u0022国家/地区\代码\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');
});
当我更改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);
}
这次我得到了有效的JSON,(根据Jsonlint)buuttt头不是application/JSON…(这很有意义,因为我发送的是响应,而不是JsonResponse…(但这正是我试图避免的,因为JsonResponse似乎在改变添加奇怪的字符)

我找到了答案

1) 只要json是有效的,内容类型不是application/json而是text/html“真的没关系”。我的JS没有播放的原因是我请求的是val,而不是val的属性,如val.msgbody.:

所以我的Javascript应该是

$.getJSON('/app_dev.php/sendjson', function(data) {
  var items = [];

  $.each(data, function(key, val) {
    items.push('<li id="' + key + '">' + val.msgbody + '</li>');
  });

  $('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo('body');
});

问题是您正在向JsonResponse传递字符串,而不是数组

您的控制器代码是:

您的控制器代码必须为:

...
return new JsonResponse(json_decode($message, true))
...

序列化是一个规范化的过程——生成一个表示对象的数组——并对该表示进行编码(即JSON或XML)。 JsonResponse为您处理编码部分(查看类的名称),因此您不能传递“序列化对象”,否则它将再次编码。 因此,解决方案只是规范化对象并将其传递给JsonResponse:

public function indexAction($id)
{
    $repository = $this->getDoctrine()->getRepository('MyBundle:Product');
    $product = $repository->find($id);

    $normalizer = new GetSetMethodNormalizer();

    $jsonResponse = new JsonResponse($normalizer->normalize($product));
    return $jsonResponse;
}

您可以仅使用无序列化程序的规范化程序来解决此问题:

    $normalizer = new ObjectNormalizer();
    $array = $normalizer->normalize($newEntry);
    $entryJSONFile = json_encode($array, JSON_UNESCAPED_UNICODE);

对象
\Symfony\Component\HttpFoundation\JsonResponse
输出JSON字符串使用函数
JSON\u encode
,通过对象
setEncodingOptions
的方法,您可以通过位常量设置输出JSON字符串选项,如
JSON\u UNESCAPED\u UNICODE

对多字节Unicode字符进行逐字编码(默认为转义为\uxxx)。从PHP5.4.0开始提供

$jsonResponse=new\Symfony\Component\HttpFoundation\jsonResponse($arrayForJSON);
$jsonResponse->setEncodingOptions($this->getEncodingOptions()| JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
$jsonResponse->send();

您的代码用json编码了两次。使用序列化程序自己进行json编码时,请使用响应类


将returnnewjsonresponse($message)替换为returnnewresponse($message)

您的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\u count”:0,“warning”:[],“error\u count”:0引号是正确的,但有些条目我不知道它们来自何处,“lastErrors、warning\u count、warnings,这些东西都不在我的数据库中。根本没有。getjson调用现在给出:[object][object]等等,很有可能是序列化过程中出现的错误。json响应实际上是一个字符串,所以在使用之前尝试将其解析为一个对象不应该真正告诉他们解码然后重新编码。这会很好地降低速度。纯代码的回答被认为是低质量的:确保提供一个解释什么你的代码可以以及如何解决问题。如果你能在你的帖子中添加更多信息,这将有助于提问者和未来的读者。另请参阅解释完全基于代码的答案:添加到我的答案信息。
...
return new JsonResponse($message)
...
...
return new JsonResponse(json_decode($message, true))
...
public function indexAction($id)
{
    $repository = $this->getDoctrine()->getRepository('MyBundle:Product');
    $product = $repository->find($id);

    $normalizer = new GetSetMethodNormalizer();

    $jsonResponse = new JsonResponse($normalizer->normalize($product));
    return $jsonResponse;
}
    $normalizer = new ObjectNormalizer();
    $array = $normalizer->normalize($newEntry);
    $entryJSONFile = json_encode($array, JSON_UNESCAPED_UNICODE);