weixin_33725239 2015-04-26 20:42 采纳率: 0%
浏览 81

ajax的JSON解析问题

This is the XMLHttpRequest object. When I put it on cosole.log it gives the following response:

console.log(this.responseText);
"[
  {
    "url": "https://api.github.com/gists/c7c0df592e99c0c34b99",
    "forks_url": "https://api.github.com/gists/c7c0df592e99c0c34b99/forks",
    "commits_url": "https://api.github.com/gists/c7c0df592e99c0c34b99/commits",
    "id": "c7c0df592e99c0c34b99",
    "git_pull_url": "https://gist.github.com/c7c0df592e99c0c34b99.git",
    "git_push_url": "https://gist.github.com/c7c0df592e99c0c34b99.git",
    "html_url": "https://gist.github.com/c7c0df592e99c0c34b99",
    "files": {
      "config.json": {
        "filename": "config.json",
        "type": "application/json",
        "language": "JSON",
        "raw_url": "https://gist.githubusercontent.com/anonymous/c7c0df592e99c0c34b99/raw/70489beaa4953f89fc8848195371da6eca76164c/config.json",
        "size": 17911
      }
    },
    "public": true,
    "created_at": "2015-04-26T20:34:11Z",
    "updated_at": "2015-04-26T20:34:11Z",
    "description": "Bootstrap Customizer Config",
    "comments": 0,
    "user": null,
    "comments_url": "h"[…]

But when I try to use JSON.parse on it it gives me an error: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.

gists = JSON.parse(this.reponseText)

im supposed to be using this api https://developer.github.com/v3/gists/ and its supposed to return valid json according to that documentation

Is the above data that was returned by the website not valid JSON? Or am I supposed to use a different function other than JSON.parse? Or what is going on? Please help.

full pastebin here: http://pastebin.com/BWttNtXP

  • 写回答

2条回答 默认 最新

  • weixin_33730836 2015-04-26 20:57
    关注

    Try this :)

        console.log(this.responseText);
        var txt = this.responseText.trim("\"");
        gists = JSON.parse(txt);
    

    this way your pastebin html works ok

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?