Php JSON.parse:JSON数据第1行第50列的JSON数据后出现意外的非空白字符

Php JSON.parse:JSON数据第1行第50列的JSON数据后出现意外的非空白字符,php,json,ajax,Php,Json,Ajax,当我使用 var jsonData = JSON.parse(xhttp.responseText); 我得到一个错误=>“JSON.parse:JSON数据第1行第50列的JSON数据后出现意外的非空白字符” 这是来自php脚本的JSON数据 {"results":[{"oldID":5,"oldMain":"News papers"}]}{"results":[{"oldID":3,"oldMain":"Construction"}]}{"results":[{"oldID":2,"ol

当我使用

 var jsonData = JSON.parse(xhttp.responseText);
我得到一个错误=>“JSON.parse:JSON数据第1行第50列的JSON数据后出现意外的非空白字符”

这是来自php脚本的JSON数据

{"results":[{"oldID":5,"oldMain":"News papers"}]}{"results":[{"oldID":3,"oldMain":"Construction"}]}{"results":[{"oldID":2,"oldMain":"Banking Files"}]}{"results":[{"oldID":1,"oldMain":"Technologies"}]}

能不能请一些人帮忙?。。。。谢谢

这不是有效的json,您必须将其包装在数组中。有效的json如下所示

[{
    "results": [{
        "oldID": 5,
        "oldMain": "News papers"
    }]
}, {
    "results": [{
        "oldID": 3,
        "oldMain": "Construction"
    }]
}, {
    "results": [{
        "oldID": 2,
        "oldMain": "Banking Files"
    }]
}, {
    "results": [{
        "oldID": 1,
        "oldMain": "Technologies"
    }]
}]  
[{
    "results": [{
        "oldID": 5,
        "oldMain": "News papers"
    }]
},
{
    "results": [{
        "oldID": 3,
        "oldMain": "Construction"
    }]
}]
查看字符串开头和结尾的括号[],以及拆分对象的

这需要一些在线json过滤器来检查您的json是否有效。

您的代码无效

你的代码

{"results":[{"oldID":5,"oldMain":"News papers"}]}{"results":[{"oldID":3,"oldMain":"Construction"}]}{"results":[{"oldID":2,"oldMain":"Banking Files"}]}{"results":[{"oldID":1,"oldMain":"Technologies"}]}
您的代码经过验证

[{"results":[{"oldID":5,"oldMain":"News papers"}]},
{"results":[{"oldID":3,"oldMain":"Construction"}]},
{"results":[{"oldID":2,"oldMain":"Banking Files"}]},
{"results":[{"oldID":1,"oldMain":"Technologies"}]}]
问题是您有多个JSON根元素。这些也不是逗号分隔的。它们还需要在[]中进行包装,这将使其成为一个对象。如果不想将响应包装在[]中,可以返回不带[]的字符串,而是这样做

JSON.parse('['+yourreponse+']') which will parse the JSON correctly.

JSON无效。如果可能的话,您可以如下更新JSON

{
"results": [{
    "oldID": 5,
    "oldMain": "News papers"
}],
"resultss": [{
    "oldID": 3,
    "oldMain": "Construction"
}]}
而且JSON不应该包含重复的关键元素。您还可以像这样将JSON绑定到JSONArray

[{
    "results": [{
        "oldID": 5,
        "oldMain": "News papers"
    }]
}, {
    "results": [{
        "oldID": 3,
        "oldMain": "Construction"
    }]
}, {
    "results": [{
        "oldID": 2,
        "oldMain": "Banking Files"
    }]
}, {
    "results": [{
        "oldID": 1,
        "oldMain": "Technologies"
    }]
}]  
[{
    "results": [{
        "oldID": 5,
        "oldMain": "News papers"
    }]
},
{
    "results": [{
        "oldID": 3,
        "oldMain": "Construction"
    }]
}]

以上答案是正确的,即您的json语法不正确。纠正语法可以解决问题(如建议的那样)

我的情况有点不同。我返回json以响应ajax调用。php使用if-else构造返回的那些JSON。我错漏了一个

if (!empty($_GET['arg1']))
  echo getTrainings($filter, 'arg1');
elseif (!empty($_GET['arg2']))
  echo getTrainings($filter, 'arg2');
if (empty($_GET['arg3']))
  echo getTrainings($filter, 'arg3');
elseif (empty($_GET['arg4']))
  echo getTrainings($filter, 'arg4');
所以事实上,返回两个json而不是一个,这导致了$.parseJSON(result)的问题


您的JSON无效。请请求JSON提供程序进行修复。你不应该做什么。