我有一个Grease Monkey脚本,它基本上从游戏中收集信息并将其发送到我的服务器以跟踪我们团队的进度。这涉及到我的GM脚本和游戏之间的大量Ajax请求,然后将一个大型转储发送到服务器。在从游戏收集数据期间,似乎一个或多个请求结果是格式不正确的JSON。我把它打印到控制台,它似乎是一个字符串而不是一个对象。我尝试了一些快速JSON.parse(JSON.stringify(r))
,但会引发unexpected end of data
错误。经过一些研究,我已经提出了下面的函数来处理Ajax响应,但记录的"对象"仍然看起来是一个字符串,仍然导致脚本的其余部分失败。
function(r){
if(typeof r === 'object'){
result = r;
}else if(typeof r === 'string'){
r.trim();
if(r.charAt(0) == '"'){
fNQ = r.indexOf('"') + 1;
lNQ = r.lastIndexOf('"');
r = r.substring(fNQ, lNQ);
r = r.trim();
}
uW.console.log(r);
result = r;
}
if(!result){
result = r;
}
}
如何确保将第三方的Ajax响应正确格式化为JSON以供脚本的其余部分使用?
编辑 - 以下是似乎触发我的错误的响应:
"
{"ok":true,"currentPage":1,"noOfPages":119,"otherAlliances":[{"allianceId":2090,"description":"Everyone has a Voice. Everyone is Equal. No Wild attacks other than that, treat other members with respect in chat, be active, nothing else but having fun your way!!!","membersCount":"77","name":"Rebels Forever","founderUserId":"18985927","might":"4003992566340","glory":"4439079","ranking":1,"hostUserId":"19002651","host":"Greywolf","hostGenderAndName":"Lord Greywolf","founderName":"GreywolfII","founderGenderAndName":"Lord GreywolfII"},{"allianceId":4428,"description":"When the world isnt what you thought. Things are not what they seemed. Phuck it take two of these mix with Alcohol and feel better in the morning.","membersCount":"92","name":"Phuckitol","founderUserId":"450033","might":"3070673000317","glory":"808848","ranking":2,"hostUserId":"450033","host":"Steelman","hostGenderAndName":"Lord Steelman","founderName":"Steelman","founderGenderAndName":"Lord Steelman"},{"allianceId":3253,"description":"Play as you like with the backing of your family. We fight as one and defend as one. Gem prizes will be handed out. Important to have fun and enjoy the game. Zilla","membersCount":"46","name":"Burn In Ashes","founderUserId":"3359434","might":"2596318711111","glory":"1914207","ranking":3,"hostUserId":"12858988","host":"gaz","hostGenderAndName":"Lord gaz","founderName":"Real'Savage","founderGenderAndName":"Lord Real'Savage"},{"allianceId":3262,"description":"Come join us and work as a team to fight and win :)","membersCount":"86","name":"Rise of Legends","founderUserId":"15149877","might":"2379467424780","glory":"2237089","ranking":4,"hostUserId":"15149877","host":"BRETT","hostGenderAndName":"Lord BRETT","founderName":"BRETT","founderGenderAndName":"Lord BRETT"},{"allianceId":3215,"description":":) zero drama tolerance, a place to fight and have fun. Is suppose to be a game after all","membersCount":"67","name":"trouble makers","founderUserId":"1887356","might":"2348999870474","glory":"2428809","ranking":5,"hostUserId":"11564038","host":"Dem0","hostGenderAndName":"Lord Dem0","founderName":"'Echo'","founderGenderAndName":"Lady 'Echo'"},{"allianceId":4924,"description":"","membersCount":"52","name":"WDW","founderUserId":"11577054","might":"2271325408341","glory":"2996503","ranking":6,"hostUserId":"11577054","host":"Nordic","hostGenderAndName":"Lord Nordic","founderName":"Nordic","founderGenderAndName":"Lord Nordic"},{"allianceId":2792,"description":"Name says it all :)","membersCount":"80","name":"Wicked MisFits","founderUserId":"18718109","might":"1984311364223","glory":"2303516","ranking":7,"hostUserId":"18718109","host":"Of_Sin","hostGenderAndName":"Lord Of_Sin","founderName":"Of_Sin","founderGenderAndName":"Lord Of_Sin"},{"allianceId":3881,"description":"","membersCount":"55","name":"LA MEUTE","founderUserId":"14440588","might":"1807184812409","glory":"862782","ranking":8,"hostUserId":"15336296","host":"lepoison","hostGenderAndName":"Lord lepoison","founderName":"LouvePoison","founderGenderAndName":"Lady LouvePoison"},{"allianceId":3273,"description":"Italians do it better.","membersCount":"80","name":"MADE in ITALY","founderUserId":"18656989","might":"1799034994799","glory":"718309","ranking":9,"hostUserId":"19009341","host":"Super_Bicio","hostGenderAndName":"Lord Super_Bicio","founderName":"Leonida","founderGenderAndName":"Lord Leonida"},{"allianceId":5283,"description":"Fight and have fun!","membersCount":"26","name":"War Crimes","founderUserId":"15105685","might":"1599775320583","glory":"6732908","ranking":10,"hostUserId":"15105685","host":"Hillz","hostGenderAndName":"Lady Hillz","founderName":"Hillz","founderGenderAndName":"Lady Hillz"}]}"
答案 0 :(得分:1)
您尝试在再次对其进行字符串化后解析该字符串。删除额外的JSON.stringify
,否则你最终会得到额外的引号。
答案 1 :(得分:0)
使用模板字符串可以正常使用。
var response = `{"ok":true,"currentPage":1,"noOfPages":119,"otherAlliances":[{"allianceId":2090,"description":"Everyone has a Voice. Everyone is Equal. No Wild attacks other than that, treat other members with respect in chat, be active, nothing else but having fun your way!!!","membersCount":"77","name":"Rebels Forever","founderUserId":"18985927","might":"4003992566340","glory":"4439079","ranking":1,"hostUserId":"19002651","host":"Greywolf","hostGenderAndName":"Lord Greywolf","founderName":"GreywolfII","founderGenderAndName":"Lord GreywolfII"},{"allianceId":4428,"description":"When the world isnt what you thought. Things are not what they seemed. Phuck it take two of these mix with Alcohol and feel better in the morning.","membersCount":"92","name":"Phuckitol","founderUserId":"450033","might":"3070673000317","glory":"808848","ranking":2,"hostUserId":"450033","host":"Steelman","hostGenderAndName":"Lord Steelman","founderName":"Steelman","founderGenderAndName":"Lord Steelman"},{"allianceId":3253,"description":"Play as you like with the backing of your family. We fight as one and defend as one. Gem prizes will be handed out. Important to have fun and enjoy the game. Zilla","membersCount":"46","name":"Burn In Ashes","founderUserId":"3359434","might":"2596318711111","glory":"1914207","ranking":3,"hostUserId":"12858988","host":"gaz","hostGenderAndName":"Lord gaz","founderName":"Real'Savage","founderGenderAndName":"Lord Real'Savage"},{"allianceId":3262,"description":"Come join us and work as a team to fight and win :)","membersCount":"86","name":"Rise of Legends","founderUserId":"15149877","might":"2379467424780","glory":"2237089","ranking":4,"hostUserId":"15149877","host":"BRETT","hostGenderAndName":"Lord BRETT","founderName":"BRETT","founderGenderAndName":"Lord BRETT"},{"allianceId":3215,"description":":) zero drama tolerance, a place to fight and have fun. Is suppose to be a game after all","membersCount":"67","name":"trouble makers","founderUserId":"1887356","might":"2348999870474","glory":"2428809","ranking":5,"hostUserId":"11564038","host":"Dem0","hostGenderAndName":"Lord Dem0","founderName":"'Echo'","founderGenderAndName":"Lady 'Echo'"},{"allianceId":4924,"description":"","membersCount":"52","name":"WDW","founderUserId":"11577054","might":"2271325408341","glory":"2996503","ranking":6,"hostUserId":"11577054","host":"Nordic","hostGenderAndName":"Lord Nordic","founderName":"Nordic","founderGenderAndName":"Lord Nordic"},{"allianceId":2792,"description":"Name says it all :)","membersCount":"80","name":"Wicked MisFits","founderUserId":"18718109","might":"1984311364223","glory":"2303516","ranking":7,"hostUserId":"18718109","host":"Of_Sin","hostGenderAndName":"Lord Of_Sin","founderName":"Of_Sin","founderGenderAndName":"Lord Of_Sin"},{"allianceId":3881,"description":"","membersCount":"55","name":"LA MEUTE","founderUserId":"14440588","might":"1807184812409","glory":"862782","ranking":8,"hostUserId":"15336296","host":"lepoison","hostGenderAndName":"Lord lepoison","founderName":"LouvePoison","founderGenderAndName":"Lady LouvePoison"},{"allianceId":3273,"description":"Italians do it better.","membersCount":"80","name":"MADE in ITALY","founderUserId":"18656989","might":"1799034994799","glory":"718309","ranking":9,"hostUserId":"19009341","host":"Super_Bicio","hostGenderAndName":"Lord Super_Bicio","founderName":"Leonida","founderGenderAndName":"Lord Leonida"},{"allianceId":5283,"description":"Fight and have fun!","membersCount":"26","name":"War Crimes","founderUserId":"15105685","might":"1599775320583","glory":"6732908","ranking":10,"hostUserId":"15105685","host":"Hillz","hostGenderAndName":"Lady Hillz","founderName":"Hillz","founderGenderAndName":"Lady Hillz"}]}`
var data = JSON.parse(response)
console.log(data)