gpt4 book ai didi

javascript - 删除部分 JSON

转载 作者:太空宇宙 更新时间:2023-11-04 03:20:13 24 4
gpt4 key购买 nike

我正在尝试解析 JSON 游戏结果统计数据并添加/更改/删除一些值:

bot.Dota2.requestMatchDetails(lobby.match_id);
bot.Dota2.on("matchDetailsData", function (matchId, matchData) {
var data = JSON.parse(JSON.stringify(matchData));
data.match.type = "partyStatistic";
data.match.partyId = bot.currentLobby.id;
for (i = 0; i < data.match.players.length; i++) {
delete data.match.players[i].party_id;
data.match.players[i].account_id = bot.Dota2.ToSteamID(data.match.players[i].account_id + "") + "";
}
delete data.vote;
delete data.result;
util.log(JSON.stringify('API STATISTIC: ' + JSON.stringify(data)));
}

我现在得到的 JSON:

{
"match": {
"duration": 647,
"startTime": 1530782586,
"cluster": 136,
"first_blood_time": 308,
"lobby_type": 1,
"human_players": 2,
"positive_votes": 0,
"negative_votes": 0,
"game_mode": "DOTA_GAMEMODE_1V1MID",
"radiant_team_score": 2,
"dire_team_score": 0,
"match_outcome": "k_EMatchOutcome_RadVictory",
"pre_game_duration": 90,
"type": "partyStatistic",
"partyId": "54333333-0bea-e611-80bc-c8600054b63d"
}
}

我需要 JSON:

{
"duration": 647,
"startTime": 1530782586,
"cluster": 136,
"first_blood_time": 308,
"lobby_type": 1,
"human_players": 2,
"positive_votes": 0,
"negative_votes": 0,
"game_mode": "DOTA_GAMEMODE_1V1MID",
"radiant_team_score": 2,
"dire_team_score": 0,
"match_outcome": "k_EMatchOutcome_RadVictory",
"pre_game_duration": 90,
"type": "partyStatistic",
"partyId": "54333333-0bea-e611-80bc-c8600054b63d"
}

我尝试在 JSON 的开头和结尾处拆分一些符号:

data = JSON.parse(JSON.stringify(data).substring(9));
data = JSON.parse(JSON.stringify(data).substring(0, data.length - 1));

但是我收到错误。还有其他解决方案吗?谢谢。

最佳答案

试试这个:

JSON.stringify(JSON.parse(data)["match"]);

或者如果数据已经是一个 json 对象

JSON.stringify(data["match"])

关于javascript - 删除部分 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51245506/

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