gpt4 book ai didi

javascript - 解析来自 3rd 方服务器的 json 时出现问题

转载 作者:行者123 更新时间:2023-12-03 07:00:31 26 4
gpt4 key购买 nike

我有一个 Grease Monkey 脚本,它基本上从游戏中收集信息并将其发送到我的服务器以跟踪我们团队的进度。这涉及到我的 GM 脚本和游戏之间的大量 Ajax 请求,然后向服务器发送一个大的转储。在从游戏收集数据的过程中,似乎一个或多个请求结果的 JSON 格式不正确。我已将其打印到控制台,它似乎是一个字符串而不是一个对象。我尝试了一些快速的 JSON.parse(JSON.stringify(r)) 但这会引发 意外的数据结束 错误。经过一番研究,我提出了下面的函数来处理 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;
}
}

如何确保第 3 方的 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"}]}"

最佳答案

您尝试在再次字符串化后解析该字符串。删除多余的 JSON.stringify,否则最终会出现多余的引号。

关于javascript - 解析来自 3rd 方服务器的 json 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37082902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com