我是 Json 和 libGDX 的新手,但我创建了一个简单的游戏,我想将玩家姓名和他们的分数存储在 Json 文件中.有没有办法做到这一点?我想在 Gdx.files.localStorage
中创建一个 Json 文件,如果它不存在,如果存在,则将新数据附加到它.
I am new to Json and libGDX but I have created a simple game and I want to store player names and their scores in a Json file. Is there a way to do this? I want to create a Json file in Gdx.files.localStorage
if it doesnt exist and if it does, append new data to it.
我已经检查了以下给出的代码:
I have checked code given at :
1>使用Json.Serializable解析Json文件
2>解析JsonlibGDX
但我找不到如何实际创建 Json 文件并向其写入多个唯一对象值(每个玩家的姓名和得分).我是否遗漏了他们的代码中的某些内容?
But I failed to locate how to actually create a Json file and write multiple unique object values (name and score of each player) to it. Did I miss something from their codes?
这个 链接 提到了如何加载现有的 json,但仅此而已.
This link mentions how to load an existing json but nothing else.
首先我不得不说我从来没有使用过Libgdx Json
API 我自己.但我会尽力帮助你.我认为 github 上的 Tutorial 应该对您有所帮助.
基本上,Json
API 允许您将整个对象写入 Json
对象,然后将其解析为 String
.为此,请使用:
First of all i have to say that i never used the Libgdx Json
API myself. But i try to help you out a bit.
I think this Tutorial on github should help you out a bit.
Basicly the Json
API allows you to write a whole object to a Json
object and then parse that to a String
. To do that use:
PlayerScore score = new PlayerScore("Player1", 1537443); // The Highscore of the Player1
Json json = new Json();
String score = json.toJson(score);
这应该是这样的:
{name: Player1, score: 1537443}
您可以使用 prettyPrint()
代替 toJson()
,它包括换行符和制表符.
Instead of toJson()
you can use prettyPrint()
, which includes linebreaks and tabs.
要将其写入文件,请使用:
To write this to a File use:
FileHandle file = Gdx.files.local("scores.json");
file.writeString(score, true); // True means append, false means overwrite.
您还可以通过实现 Json.Serializable
或使用 writeValue
手动添加值来自定义您的 Json
.
You can also customize your Json
by implementing Json.Serializable
or by adding the values by hand, using writeValue
.
阅读类似:
FileHandle file = Gdx.files.local("scores.json");
String scores = file.readString();
Json json = new Json();
PlayerScore score = json.fromJson(PlayerScore.class, scores);
如果您一直通过实现 Json.Serializable
来使用自定义版本,则您已经实现了 read (Json json, JsonValue jsonMap)
方法.如果你正确地实现了它,你的反序列化应该可以工作.如果您一直在手动添加值,则需要创建一个 JsonValue
jsonFile = new JsonValue(scores)
.scores
是 File
的 String
.现在您可以循环遍历此 JsonValue
的子项或按名称获取其子项.
If you have been using a customized version by implementing Json.Serializable
you have implemented the read (Json json, JsonValue jsonMap)
method. If you implemented it correctly you the deserialization should work. If you have been adding the values by hand you need to create a JsonValue
jsonFile = new JsonValue(scores)
. scores
is the String
of the File
. Now you can cycle throught the childs of this JsonValue
or get its childs by name.
最后一件事:对于高分或类似的事情可能是 Libgdx Preferences
是更好的选择.这里您可以阅读如何使用它们.
One last thing: For highscores or things like that maybe the Libgdx Preferences
are the better choice. Here you can read how to use them.
希望我能帮上忙.
这篇关于使用 libGDX 写入 Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!