我正在努力研究如何读取位于.json //< worldname >/stats/< UUID >.json中的统计信息。
Python版本
import json
json_file = open("C:/Users/<name>/AppData/Roaming/.minecraft/saves/<worldname>/stats/<UUID>.json")
variables = json.load(json_file)
json_file.close()
print(variables["stats"])Java版本:
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try (Reader reader = new FileReader("C:/Users/<name>/AppData/Roaming/.minecraft/saves/<worldname>/stats/<UUID>.json")) {
JSONObject jsonObject = (JSONObject) parser.parse(reader);
System.out.println(jsonObject);
String stats = (String) jsonObject.get("stats");
System.out.println(stats);
//long age = (Long) jsonObject.get("age");
//System.out.println(age);
// loop array
//JSONArray msg = (JSONArray) jsonObject.get("messages");
//Iterator<String> iterator = msg.iterator();
//while (iterator.hasNext()) {
// System.out.println(iterator.next());
//}
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}以下是文件中.json文件的大致样子:
{'stats': {'minecraft:mined': {'minecraft:lily_of_the_valley': 1, 'minecraft:birch_leaves': 2, 'minecraft:crafting_table': 2, 'minecraft:grass': 1, 'minecraft:grass_block': 4, 'minecraft:oak_log': 10, 'minecraft:dirt': 6, 'minecraft:stone': 6, 'minecraft:oak_leaves': 3}, 'minecraft:killed': {'minecraft:pig': 4}, 'minecraft:picked_up': {'minecraft:stick': 2, 'minecraft:crafting_table': 3, 'minecraft:wooden_pickaxe': 1, 'minecraft:porkchop': 16, 'minecraft:oak_log': 10, 'minecraft:lily_of_the_valley': 1, 'minecraft:dirt': 19, 'minecraft:stone_axe': 1, 'minecraft:stone_pickaxe': 1, 'minecraft:oak_sapling': 2, 'minecraft:birch_sapling': 1, 'minecraft:oak_planks': 16, 'minecraft:cobblestone': 6}, 'minecraft:custom': {'minecraft:time_since_rest': 2832, 'minecraft:sprint_one_cm': 26594, 'minecraft:damage_taken': 360, 'minecraft:walk_one_cm': 10606, 'minecraft:mob_kills': 4, 'minecraft:drop': 1, 'minecraft:damage_dealt': 390, 'minecraft:swim_one_cm': 6359, 'minecraft:interact_with_crafting_table': 2, 'minecraft:fly_one_cm': 37248, 'minecraft:crouch_one_cm': 459, 'minecraft:play_one_minute': 6532, 'minecraft:deaths': 1, 'minecraft:sneak_time': 460, 'minecraft:walk_under_water_one_cm': 38, 'minecraft:jump': 293, 'minecraft:leave_game': 1, 'minecraft:walk_on_water_one_cm': 521, 'minecraft:time_since_death': 2811, 'minecraft:fall_one_cm': 9542}, 'minecraft:dropped': {'minecraft:birch_sapling': 1}, 'minecraft:crafted': {'minecraft:wooden_sword': 1, 'minecraft:oak_planks': 40, 'minecraft:crafting_table': 1, 'minecraft:stick': 8, 'minecraft:wooden_pickaxe': 1, 'minecraft:stone_pickaxe': 1, 'minecraft:stone_axe': 1}, 'minecraft:used': {'minecraft:oak_planks': 27, 'minecraft:wooden_pickaxe': 6, 'minecraft:stone_axe': 7, 'minecraft:wooden_sword': 13, 'minecraft:lily_of_the_valley': 1, 'minecraft:crafting_table': 2, 'minecraft:porkchop': 4}}, 'DataVersion': 2586}因此,我想知道如何访问Java或Python中每一节中的每个变量,这将指导我编写统计读取器的路径。我正在为我的软件、设计和开发类编写一个统计读取器程序,我想不出如何进一步访问.json文件,只能访问"stats“元素中的内容。因此,一般来说,我希望程序能够将每个变量排序到它们的子标题中(“我的经验之谈:挖掘的”),然后通过稍后的搜索在该子标题中获得变量,但我稍后会讲到,我只需要弄清楚如何读取和使用.json文件。谢谢大家给我的帮助,即使这是一个正确的方向的提示,为自己研究如何做到这一点,因为我不知道从哪里开始。
不管怎样,谢谢你们的帮助!
发布于 2021-02-18 21:58:50
json.load()将json转换为python和list,因此您可以像处理常规python数据结构一样处理这些数据。(如果您需要更新这些内容,请考虑搜索python list和python dict。)
基本上,在你的例子中,你可以做的是:
stats = variables['stats']
# see what that gets you
print(stats)
mined = stats['minecraft:mined']
# once more, see what that is
print(mined)
# and so forth:
print(f"stone: {mined['minecraft:stone']}")从那以后,你就可以自己继续了。:)
发布于 2021-02-19 16:44:26
尽管它已经解决了,但我认为在Java中看到它对您也有好处。看看这个示例Java-Json。
为了简单起见,我将数据更改为{'stats': {'minecraft:mined': {'minecraft:lily_of_the_valley': 1}}}
这样做的方法如下:
public static void main(String[] args) throws ParseException{
String s = "{\"stats\": {\"minecraft:mined\": {\"minecraft:lily_of_the_valley\": 1}}}";
JSONParser parser = new JSONParser();
Object obj = parser.parse(s);
JSONObject base = (JSONObject) obj;
JSONObject stats = (JSONObject) base.get("stats");
JSONObject minecraft_mined = (JSONObject) stats.get("minecraft:mined");
long minecraft_lily_of_the_valley = (long) minecraft_mined.get("minecraft:lily_of_the_valley");
System.out.println(base);
System.out.println(stats);
System.out.println(minecraft_mined);
System.out.println(minecraft_lily_of_the_valley);
}https://stackoverflow.com/questions/66268603
复制相似问题