如何将数据从安卓游戏保存到本地JSON文件。

huangapple go评论63阅读模式
英文:

How i save data from android game to a local json file

问题

我想在内部存储中将用户的数据保存到JSON文件中,但它不起作用,问题是什么?这是代码...................................................................................................................

public JSONObject addToJson(Context context, String email, String password) throws Exception {
    String id_P = UUID.randomUUID().toString();
    JSONObject jsonObject = new JSONObject();

    jsonObject.put("id_P", id_P);
    jsonObject.put("Email", email);
    jsonObject.put("password", password);

    String j = jsonObject.toString();
    System.out.println(j);
    File file = new File(context.getFilesDir(), "dbApp.json");
    FileWriter fileWriter = new FileWriter(file);
    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
    bufferedWriter.write(j);
    bufferedWriter.close();

    return jsonObject;
}
英文:

i want to save the user's data in json file in a internel memory but it doesn't work what's the problem ???? this is the code ...................................................................................................................

public JSONObject addToJson(Context context, String email,String password)throws Exception{
    String id_P = UUID.randomUUID().toString();
    JSONObject jsonObject = new JSONObject();

    jsonObject.put("id_P",id_P);
    jsonObject.put("Email",email);
    jsonObject.put("password",password);

    
    String j = jsonObject.toString();
    System.out.println(j);
    File file = new File(context.getFilesDir(),"dbApp.json");
    FileWriter fileWriter = new FileWriter(file);
    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
    bufferedWriter.write(j);
    bufferedWriter.close();

    return jsonObject;
}

答案1

得分: 0

你可以使用不同的本地数据库,如Room、Realm等。但是对于

写入:您需要将JSON转换为字符串或模型类,然后可以存储到数据库中。
阅读:将字符串转换为JSON或模型类,然后使用它。

更多详细信息,请查看
https://realm.io/docs/java/latest/

Android中Realm数据库的示例,您可以选择使用:

写入

User user = realm.createObject(User.class); // 创建一个新对象
user.setName("John");
user.setEmail("john@corporation.com");
realm.commitTransaction();

阅读

RealmQuery query = realm.where(User.class);

// 添加查询条件:
query.equalTo("name", "John");
query.or().equalTo("name", "Peter");

// 执行查询:
RealmResults result1 = query.findAll();

// 或者可以一次完成相同的操作(“Fluent interface”方式):
RealmResults result2 = realm.where(User.class)
.equalTo("name", "John")
.or()
.equalTo("name", "Peter")
.findAll();

英文:

You can use different of local database like Room, Realm etc. But for

Writing:  you need to convert your JSON into String or Model class and then you can store into Database. 
Reading  : convert string into JSON or Model Class and use the same.

Take more look at
https://realm.io/docs/java/latest/

Example of Realm Database in Android You can option of choose this :

Writing

User user = realm.createObject(User.class); // Create a new object
user.setName("John");
user.setEmail("john@corporation.com");
realm.commitTransaction();

Reading

RealmQuery<User> query = realm.where(User.class);

// Add query conditions:
query.equalTo("name", "John");
query.or().equalTo("name", "Peter");

// Execute the query:
RealmResults<User> result1 = query.findAll();

// Or alternatively do the same all at once (the "Fluent interface"):
RealmResults<User> result2 = realm.where(User.class)
                              .equalTo("name", "John")
                              .or()
                              .equalTo("name", "Peter")
                              .findAll();

答案2

得分: 0

Here's the translated content:

我并不是这方面的专家但我认为您应该这样做

首先在您的 build.gradle 中添加这个 JSON 转换库

    implementation 'com.google.code.gson:gson:2.8.6';

然后您需要创建一个模型类在其中应该将您的数据分类到不同的映射中注意始终使用映射数组列表可能无法正常工作

    public class Model {
        Map<String, String> userData;
        
        public Model(Map<String, String> userData) {
            this.userData = userData;
        }
        
        public void setUserData(Map<String, String> userData) {
            this.userData = userData;
        }
    }

好的然后要设置数据您应该创建一个 Map然后初始化这个类并将 Map 传递给它类似于这样

    Map<String, String> userData = new HashMap<>();
    userData.put("username", "something");
    userData.put("password", "something");
    Model model = new Model(userData);

现在您可以轻松地将其转换为 JSON
Gson gson = new Gson();
String json = gson.toJson(model);

然后您可以以文本格式轻松保存这个 JSON 到文件中

Please let me know if there's anything else I can assist you with.

英文:

well I am not an expert in this matter but I think here is what you should do:

first add this JSON converter library to your build.gradle:

implementation &#39;com.google.code.gson:gson:2.8.6&#39;

after that you need to create a model class in which you should categorize your data in different maps , NOTE: use maps all the time array list won't work properly:

Class Model{
Map&lt;String ,String&gt; userData;

 public Model(Map&lt;String, String&gt; setUserdata) {
    this.setUserdata = setUserdata;
}


         public void setSetUserdata(Map&lt;String, String&gt; setUserdata) {
          this.setUserdata = setUserdata;
          }


}  

okay , and then for setting data you should create a Map and then initialize the class and pass it the Map similar to this:

Map&lt;String ,String&gt; userData=new HashMap&lt;&gt;();
        setUserdata.put(&quot;username&quot;,&quot;something&quot;);
        setUserdata.put(&quot;password&quot;,&quot;something&quot;);
Model model=new Model(userData);

and now you can easily convert this to a JSON :
Gson gson = new Gson();
String json = gson.toJson(model);

and you can easily save this in file with text format.

huangapple
  • 本文由 发表于 2020年8月5日 18:15:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63263008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定