【问题标题】:JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 50 of the JSON dataJSON.parse:JSON 数据第 1 行第 50 列的 JSON 数据后出现意外的非空白字符
【发布时间】:2018-07-04 11:17:28
【问题描述】:

当我使用....

 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 提供程序修复它。什么都不应该做。

标签: php json ajax


【解决方案1】:

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"
    }]
}]

【讨论】:

    【解决方案2】:

    它不是一个有效的 json,你必须将它包装在数组中。有效的json是这样的

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

    看一下 string 开头和结尾的括号 [ ], 来分割对象。

    这是一些在线 json linter here 来检查你的 json 是否有效。

    【讨论】:

      【解决方案3】:

      您的代码无效。

      你的代码;

      {"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.
      

      【讨论】:

        【解决方案4】:

        以上答案是正确的,即您的 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); 出现问题

        var par = $.parseJSON(result);
        

        【讨论】:

          【解决方案5】:

          聚会有点晚了,但@igniz87 和@Benjamin James Kippax 的回答让您的网站面临安全问题,效仿他们的做法非常危险 . 正如 Owasp 所说,

          ALWAYS RETURN JSON WITH AN OBJECT ON THE OUTSIDE
          

          因此,为了保护层免受黑客的野蛮攻击,您需要始终让外部原语成为 JSON 字符串的对象。如您所见,上述人员的回答不符合这一重要的安全规则。

          此外,Owasp 还举了一个例子,说像下面这样的 JSON 是EXPLOITABLE

          [{"object": "inside an array"}]
          

          然而,以下 JSON 不是,

          {"object": "not inside an array"}
          {"result": [{"object": "inside an array"}]}
          

          您需要删除重复的关键元素并按以下方式更改它,以便在外面有一个对象。

          {
          "results": [{
              "oldID": 5,
              "oldMain": "News papers"
          }],
          "resultss": [{
              "oldID": 3,
              "oldMain": "Construction"
          }]}
          

          在这里,实际上您应该带上 php 代码,因为问题在于发送 JSON 的 php 代码。当您想发送 JSON 时,请尝试创建一个数据数组。我按照以下方式进行。

                  $data = [];
                  foreach ($results as $res) {
                      $t = [];
                      $t['oldID'] = $res['oldID'];
                      $t['oldMain'] = $res['oldMain'];
                      $data[] = $t;
                  }
                  echo json_encode(['results' => $data]);
          

          你最好把这里缺少的 php 代码发给你。我的 php 脚本不包含你所有的代码,但它让你知道如何去做。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-02-16
            • 1970-01-01
            • 2021-09-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-01-30
            相关资源
            最近更新 更多