英文:
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.equalTo("name", "John");
query.or().equalTo("name", "Peter");
// 执行查询:
RealmResults
// 或者可以一次完成相同的操作(“Fluent interface”方式):
RealmResults
.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 '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:
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<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();
String json = gson.toJson(model);
and you can easily save this in file with text format.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论