Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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
Jquery 使用不同对象数组的json响应_Jquery_Arrays_Ajax_Json - Fatal编程技术网

Jquery 使用不同对象数组的json响应

Jquery 使用不同对象数组的json响应,jquery,arrays,ajax,json,Jquery,Arrays,Ajax,Json,我想发送一个对包含两个不同对象数组的ajax调用的响应。 当我设定 dataType: "json", ajax调用失败,因此我认为控制器发送的格式是错误的 $serializer = $this->container->get('jms_serializer'); $json2 = $serializer->serialize($readyproblems, 'json', SerializationContext::create()->enabl

我想发送一个对包含两个不同对象数组的ajax调用的响应。 当我设定

 dataType: "json", 
ajax调用失败,因此我认为控制器发送的格式是错误的

$serializer = $this->container->get('jms_serializer');
        $json2 = $serializer->serialize($readyproblems, 'json', SerializationContext::create()->enableMaxDepthChecks());
        $json1 = $serializer->serialize($result, 'json', SerializationContext::create()->enableMaxDepthChecks());
        $json = '{"data1": ' . $json1 . '"data2":' . $json2 . '}';
        return new Response($json);
这会回来的

{"data1": [{"id":4,"Description":"solution 4"},{"id":1,"Description":"sol 1"}]"data2":[{"id":1,"Description":"problima1"},{"id":2,"Description":"problima2"}]}
在ajax成功之后,我希望得到如下数据

function callback(data) {
data.data1[0].property
data.data2[1].property 
}

还有
JSON.parse(数据)导致错误,因为字符串不正确

在“数据2”之前缺少逗号

下面是如何修复您的代码

$serializer = $this->container->get('jms_serializer');
$json2 = $serializer->serialize($readyproblems, 'json', SerializationContext::create()->enableMaxDepthChecks());
$json1 = $serializer->serialize($result, 'json', SerializationContext::create()->enableMaxDepthChecks());
$json = '{"data1": ' . $json1 . ',"data2":' . $json2 . '}';
return new Response($json);

“data2”之前缺少逗号
$serializer = $this->container->get('jms_serializer');
$json2 = $serializer->serialize($readyproblems, 'json', SerializationContext::create()->enableMaxDepthChecks());
$json1 = $serializer->serialize($result, 'json', SerializationContext::create()->enableMaxDepthChecks());
$json = '{"data1": ' . $json1 . ',"data2":' . $json2 . '}';
return new Response($json);