javascript - Symfony2 JSON 对象 - 错误未捕获 TypeError : Cannot use 'in' operator to search for '636'

标签 javascript php json ajax symfony

我正在尝试循环遍历 Symfony2 中的操作返回的 JSON 数据对象的内容。
无论我使用 $.getJSON 还是 $.ajax 仍然会抛出错误:“Uncaught TypeError:无法使用 'in' 运算符来搜索‘636’”
下面是 AJAX 请求和返回的数据:`

$(document).ready(function () {
$('a.parents ').on('click', function (e) {
e.preventDefault();
var newRow = '<table>\n\
<thead>\n\
<tr>\n\
<th>Nom</th>\n\
<th>Prenom(s)</th>\n\\n\
</tr>\n\
</thead>\n\
';
$.getJSON(Routing.generate($(this).attr('data-href'),{id: $(this).attr('data-id')}),
function(data) {
$.each(data.parents, function (i, parent) {
    newRow += '<tr><td>' + parent[i].nom + '</td><td>' + parent[i].prenoms + '</td></tr>';
});
newRow += '</table>';
alert(newRow);
},
'json'); 
});
});
`

Action返回的示例结果:
{"成功":true," parent ":"{\u0022nom\u0022:\u0022Dafn\\u00e9e\u0022,\u0022prenoms\u0022:\u0022Graham\u0022},{\u0022nom\u0022:\u0022G\\u00e9raldine\u0022,\u0022prenoms\u0022:\u0022Phillips\u0022},{\u0022nom\u0022:\u0022H\\u00e9l\\u00e8na\u0022,\u0022prenoms\u0022:\u0022埃尔南德斯\u0022 },{\u0022名义\u0022:\u0022Ma\\u00eblla\u0022,\u0022prenoms\u0022:\u0022Perez\u0022},{\u0022nom\u0022:\u0022Ilustr\\u00e9e\u0022,\u0022prenoms\u0022:\u0022Dixon\u0022 },{\u0022名义\u0022:\u0022Ru\\u00ec\u0022,\u0022prenoms\u0022:\u0022格里芬\u0022},{\u0022nom\u0022:\u0022Rach\\u00e8le\u0022,\u0022prenoms\u0022:\u0022肯尼迪\u0022},{\u0022名义\u0022:\u0022B\\u00e9n\\u00e9dicte\u0022,\u0022prenoms\u0022:\u0022弗 Gson \u0022},{\u0022nom\u0022:\u0022Gis\\u00e8le\u0022,\u0022prenoms\u0022:\u0022My呃\u0022},{\u0022名义\u0022:\u0022Laur\\u00e9lie\u0022,\u0022prenoms\u0022:\u0022Mason\u0022},{\u0022nom\u0022:\u0022Y\\u00e9nora\u0022,\u0022prenoms\u0022:\u0022Olson\u0022},{\u0022nom\u0022:\u0022L\\u00e9onie\u0022,\u0022prenoms\u0022:\u0022Burns\u0022},{\u0022nom\u0022:\u0022Bj\\u00f6rn\u0022,\u0022prenoms\u0022:\u0022V阿斯奎兹\u0022},{\u0022nom\u0022:\u0022M\\u00e9lia\u0022,\u0022prenoms\u0022:\u0022巴特勒\u0022},{\u0022nom\u0022:\u0022R\\u00e1o\u0022,\u0022prenoms\u0022:\u0022阿姆斯特朗\u 0022}]"}
我正在使用JMS Serializer Bundle在我的行动中如下:

public function trainingCandidatesAction(Request $request,$id) {
        $request = $this->get('request');
    try{
     if ($request->isXmlHttpRequest() || $request->getMethod() == 'GET' || $request->getMethod() == 'POST') {
            $em = $this->getDoctrine()->getManager();
            $array = $em->getRepository('ParentsBundle:Parents')->getParentsByFormation($id);

            $serializer = $this->container->get('jms_serializer');
            $jsonContent = $serializer->serialize($array,'json');
            }

            return new JsonResponse(array('success'=>true, 'parents'=>$jsonContent));
    }catch(\Execption $exception)
    {
         return new JsonResponse(array(
            'success' => false,
            'code'    => $exception->getCode(),
            'message' => $exception->getMessage(),
        ));
    }

        }

我已经通过 stackoverflow 上的类似问题了解了各种建议的方法,但无法得到正确的答案,我一定错过了一些东西。

最佳答案

首先,要获得干净的 JSON,您需要在渲染 JsonResponse 之前解码数据:

return new JsonResponse(array(
    'success'=>true, 
    'parents'=> json_decode($jsonContent),
);

这是因为您的数据已经序列化,并且 JsonResponse 再次对它们进行编码。

然后,如果这不能解决您的错误,请尝试在使用之前解析 json,例如:

$.each(JSON.parse(data.parents), function(i, parent) {
    // ...  
});

希望这对您有帮助。

关于javascript - Symfony2 JSON 对象 - 错误未捕获 TypeError : Cannot use 'in' operator to search for '636' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36135174/

相关文章:

javascript - 添加幻灯片切换

php - 正则表达式排除标题标签之间的内容

php - laravel route 使用了什么概念

javascript - 通过 jQuery 将 JSON 对象转换为格式化数组

javascript - 将 JSON 字符串转换为函数参数

javascript - 我想将 JSON 值拆分为两个值

Javascript - 以编程方式创建桌面链接

javascript - 如何在 Chrome DevTools 中调试 ajax 请求中加载的 js

javascript - 如何在 React 中将 html 表格单元格数据动态插入到具有动态标题的表格中?

php - 来自带链接的 JSON 的 AutoSuggest