【问题标题】:Issue parsing JSON arrays (unexpected end / undefined xhr.responseText)解析 JSON 数组的问题(意外结束/未定义的 xhr.responseText)
【发布时间】:2018-02-11 23:48:58
【问题描述】:
  {
 "requirements": [
  {
   "name": {
      "required":true,
      "type":"string",
      "length":{"min":3}
    }
   },
    {
    "phone": {
      "type":"number",
      "required":true
    }
   }
 ],
 "people": [
  {
    "id":1,
    "name":"Jim",
    "Phone":0123456789
  },
  {
    "id":2,
    "name":"Jack",
    "Phone":4738383838
  }
 ]
}

function request (method, url) {
  var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function() {
          if (xhr.readyState == 4 && xhr.status == 200)
           val = JSON.parse(xhr.responseText);
                console.log (val.people[1]);
      }
      xhr.open(method, url, true);
      xhr.send(null);
}

我在解析此 JSON 时遇到问题。在它无效之前,但我修复了它。现在,它以未定义的形式出现,或者它告诉我 JSON 出现了意外的结束。我不确定 val.people[1] 是否是正确的方法。

【问题讨论】:

    标签: javascript arrays json xmlhttprequest


    【解决方案1】:

    解析器在以下整数的前导零上绊倒:0123456789

    它把它当作一个八进制数。

    【讨论】:

      猜你喜欢
      • 2021-06-04
      • 2018-03-15
      • 2020-07-29
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 1970-01-01
      相关资源
      最近更新 更多