无法解析用户脚本中的json [重复]

问题描述 投票:0回答:1

我是JavaScript的新手,我可以说bash脚本比这容易。我想从网址获取json文件,然后解析以获取ID列表。我在Android上使用移动Firefox。在bash脚本中,您编写curl "your url" | jq '.id'。然后,您从json文件中获取ID。这是我的代码

// ==UserScript==
// @name qwerty
// @namespace http://use.i.E.your.homepage/
// @version 0.1
// @description enter something useful
// @match http*://*
// @grant        GM_xmlhttpRequest
// @require http://code.jquery.com/jquery-3.4.1.min.js
// @copyright 2012+, You
// ==/UserScript==

//Test if script not work
var b = "asdf"
alert(b)
alert(11);

//Script gets file from json url
GM_xmlhttpRequest({
  method: "GET",
  url: "your url to json",

  onload: processJSON_Response
});


//Site response test
function processJSON_Response(rspObj) {
  if (rspObj.status != 200 && rspObj.status != 304) {
    reportAJAX_Error(rspObj);
    return;
  }
  //Alert to check response is correct or no
  alert(rspObj.response)
  var a = rspObj.response
  //This code not work. It's json parsing
  var id = a.id
  //Alert id list of json
  alert(id)
}

测试json文件

{"updated_at":"2020-04-27T18:52:13.107-04:00","id":[637,677,5765,202]}
jquery json userscripts
1个回答
0
投票

您需要致电JSON.parse

var a= JSON.parse(rspObj.response);
© www.soinside.com 2019 - 2024. All rights reserved.