Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
加载多个JSON对象_Json_Ajax - Fatal编程技术网

加载多个JSON对象

加载多个JSON对象,json,ajax,Json,Ajax,我试图从一个文件加载多个JSON对象,但尝试失败。 下面是我的代码,当我试图加载.JSON文件时,代码运行出错 $(document).ready(function(){ $.ajax({ url: "..data.json", method: "GET", success: function(data) { // do something }, error: function(

我试图从一个文件加载多个JSON对象,但尝试失败。 下面是我的代码,当我试图加载.JSON文件时,代码运行出错

$(document).ready(function(){
    $.ajax({
        url: "..data.json", 
        method: "GET", 
        success: function(data) {
           // do something
        },  
        error: function(data) {
            console.log('error');
        }
    });
  });
我尝试加载的文件格式如下所示:

[{“id_-first”:“1”,“data_-first”:“1”},{“id_-first”:“2”,“data_-first”:“2”}][{“id_-second”:“1”,“data_-second”:“1”},{“id_-second”:“2”,“data_-second”:“2”}]


这个问题有什么解决办法吗?提前感谢您的帮助

我认为您需要在后端像这样更改json模式

[
 [{
    "id_first": "1",
    "data_first": "1"
  }, {
    "id_first": "2",
    "data_first": "2"
 }],
 [{
    "id_second": "1",
    "data_second": "1"
 }, {
    "id_second": "2",
    "data_second": "2"
 }]
]

处理数据更容易。

检查您的url。对我来说似乎不合理。错误是由ajax请求生成的,而不是json文件。同样,类似于JSON的字符串包含2个不同的JSON。data.JSON位于tmp文件夹中,请尝试不使用类似data.JSON的点。当我尝试只加载第一个对象时({“id_first”:“1”,“data_first”:“1”},{“id_first”:“2”,“data_first”:“2”}]该函数运行良好,但关键是要处理第二个对象。感谢您的帮助,问题用您的格式解决了。我还有一个问题。虽然该函数加载了json文件,但我不能将这些文件推送到变量。您有什么建议吗?以下是我的尝试:`var data_second=[];for(var I in data){data_second.push(data[i].data_second);console.log(data[i].data_second);`我认为您无法推送它。只需从api中解决json模式。