将json输出中的特定值列表存储到Python中的文本文件中



我正在使用翼手龙来托管我的minecraft服务器,我一直在使用API来制作一个工具来控制它,这样我就不必去托管它的机器上,也不必登录minecraft。我希望能够从api发出的json输出中获取服务器名称和它们各自的UUID,并将它们存储在一个文件中,以便在我需要指定要对哪个服务器执行某个操作时可以引用它们。有没有一种方法可以像这样制作一个过滤列表,并在我想执行操作时引用它?

以下是输出示例:

{
"object":"list",
"data":[
{
"object":"server",
"attributes":{
"server_owner":False,
"identifier":"f7c8518a",
"internal_id":1,
"uuid":"f7c8518a-3909-4f34-8e42-c16e66054480",
"name":"Minecraft Server",
"node":"Server node",
"sftp_details":{
"ip":"127.0.0.1",
"port":2022
},
"description":"",
"limits":{
"memory":5333,
"swap":-1,
"disk":0,
"io":500,
"cpu":0
},
"invocation":"java -Xms128M -Xmx5333M -jar forge-1.12.2-14.23.5.2854.jar",
"egg_features":[
"eula"
],
"feature_limits":{
"databases":0,
"allocations":0,
"backups":0
},
"is_suspended":False,
"is_installing":False,
"relationships":{
"allocations":{
"object":"list",
"data":[
{
"object":"allocation",
"attributes":{
"id":1,
"ip":"The IP address",
"ip_alias":"A Minecraft Server",
"port":25565,
"notes":"None",
"is_default":True
}
}
]
},
"variables":{
"object":"list",
"data":[
{
"object":"egg_variable",
"attributes":{
"name":"Server Jar File",
"description":"The name of the Jarfile to use when running Forge Mod.",
"env_variable":"SERVER_JARFILE",
"default_value":"server.jar",
"server_value":"forge-1.12.2-14.23.5.2854.jar",
"is_editable":True,
"rules":"required|regex:/^([\w\d._-]+)(\.jar)$/"
}
},
{
"object":"egg_variable",
"attributes":{
"name":"Forge version",
"description":"The version of minecraft you want to install for.rnrnLeaving latest will install the latest recommended version.",
"env_variable":"MC_VERSION",
"default_value":"latest",
"server_value":"1.12.2",
"is_editable":True,
"rules":"required|string|max:9"
}
},
{
"object":"egg_variable",
"attributes":{
"name":"Build Type",
"description":"The type of server jar to download from forge.rnrnValid types are "recommended" and "latest".",
"env_variable":"BUILD_TYPE",
"default_value":"recommended",
"server_value":"recommended",
"is_editable":True,
"rules":"required|string|max:20"
}
},
{
"object":"egg_variable",
"attributes":{
"name":"Forge Version",
"description":"Gets an exact version.rnrnEx. 1.15.2-31.2.4rnrnOverrides MC_VERSION and BUILD_TYPE. If it fails to download the server files it will fail to install.",
"env_variable":"FORGE_VERSION",
"default_value":"",
"server_value":"1.12.2-14.23.5.2854",
"is_editable":True,
"rules":"required|string|max:20"
}
}
]
}
}
}
}
],
"meta":{
"pagination":{
"total":1,
"count":1,
"per_page":50,
"current_page":1,
"total_pages":1,
"links":{

}
}
}
}
DataVar = { JSON }
list_data = [x["attributes"]["name"] + "," + x["attributes"]["uuid"] for x in DataVar["data"] if x["attributes"]["name"] and x["attributes"]["uuid"]]
with open('server.csv', 'w+') as file:
file.writelines(list_data)

请查看我的评论。你的要求很简单,但如果这就是你所需要的,那就去吧。虽然我会在Docker中使用Pterodactyl使用的东西。

Pterodactyl是一个服务器管理平台,它使用Docker容器来管理应用程序的实例。它设计用于运行、配置和管理无头游戏服务器,如Minecraft服务器,但也可用于其他应用程序

howeverYoureGettingTheJSONDataVar = { YOUR JSON HERE }
#ALSO YOU SEE THAT BIG 0 BELOW...IT LOOKS LIKE [0] YEAH... THERE ARE MULTIPLE ARRAYS THAN YOU'LL NEED TO USE A LOOP
dataArr = howeverYoureGettingTheJSONDataVar["data"][0]
_IAmLazy = open("A_List_Of_STUFF.csv", "w+") 
_IAmLazy.write(dataArr["attributes"]["name"] + "," + dataArr["attributes"]["uuid"] )
_IAmLazy.close

最新更新