写使用libGDX的Json [英] Write to Json using libGDX

查看:282
本文介绍了写使用libGDX的Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的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.

我已经检查了在给定的code:

I have checked code given at :

1> <一个href="https://$c$c.google.com/p/steigert-libgdx/source/browse/trunk/tyrian-game/src/com/blogspot/steigert/tyrian/domain/Profile.java?spec=svn18&r=19"相对=nofollow称号=链接>使用Json.Serializable来解析JSON文件

2> 解析JSON在libGDX

但我没能找到如何实际创建一个JSON文件,并写入多个独特的对象值(姓名和分数每个球员的)给它。我错过了什么,从他们的codeS?

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教程应该帮你出位。照片 Basicly的的Json API允许你写一个整体对象到的Json 对象,然后解析到一个字符串。要做到这一点使用:

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}

而不是的toJSON的()您可以使用 prettyPrint(),其中包括换行符和制表符。

Instead of toJson() you can use prettyPrint(), which includes linebreaks and tabs.

要写到文件中使用:

FileHandle file = Gdx.files.local("scores.json");
file.writeString(score, true);         // True means append, false means overwrite.

您还可以自定义你的的Json 通过实施 Json.Serializable 或通过手动添加值,使用 writeValue

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 您已经实现了阅读(JSON JSON,JsonValue jsonMap)方法。如果你实现它正确,你反序列化应该工作。如果你一直在增加值的手,你需要创建一个的 JsonValue jsonFile =新JsonValue(分数)分数字符串的文件。现在,您可以循环throught这个孩子的 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 JsonValuejsonFile = 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆