[英]How i save data from android game to a local json file
i want to save the user's data in json file in a internel memory but it doesn't work what's the problem????我想将用户的数据保存在 json 文件中的内部 memory 但它不起作用有什么问题???? 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;
}
You can use different of local database like Room, Realm etc. But for您可以使用不同的本地数据库,如 Room、Realm 等。但是对于
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/多看看https://realm.io/docs/java/latest/
Example of Realm Database in Android You can option of choose this: Android 中的 Realm 数据库示例 您可以选择以下选项:
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();
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:首先将此 JSON 转换器库添加到您的 build.gradle:
implementation 'com.google.code.gson:gson:2.8.6'
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:之后,您需要创建一个 model class ,您应该在其中对不同地图中的数据进行分类,注意:始终使用地图数组列表将无法正常工作:
Class Model{
Map<String ,String> userData;
public Model(Map<String, String> setUserdata) {
this.setUserdata = setUserdata;
}
public void setSetUserdata(Map<String, String> 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 ,然后初始化 class 并将其传递给 Map ,类似于:
Map<String ,String> userData=new HashMap<>();
setUserdata.put("username","something");
setUserdata.put("password","something");
Model model=new Model(userData);
and now you can easily convert this to a JSON: Gson gson = new Gson();现在您可以轻松地将其转换为 JSON: Gson gson = new Gson(); String json = gson.toJson(model);
字符串 json = gson.toJson(model);
and you can easily save this in file with text format.您可以轻松地将其保存在文本格式的文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.