英文:
Issue writing to json file in android
问题
我尝试写入一个 JSON 文件,但它就是不起作用,我没有收到任何错误消息,但文件中什么也没有改变。
以下是我的代码的相关部分:
try {
// 方法1
Gson gson = new Gson();
String s = "gjsanhfhj";
String path = this.getApplicationContext().getFilesDir().getAbsolutePath() + "/" + "test.json";
File file = new File(path);
file.setWritable(true);
gson.toJson(s, new FileWriter(file));
// 方法2
FileOutputStream outputStream;
outputStream = openFileOutput(path, Context.MODE_PRIVATE);
outputStream.write(s.getBytes());
outputStream.close();
// 方法3
Writer output;
output = new BufferedWriter(new FileWriter(file));
output.write(s);
output.close();
Toast.makeText(this.getApplicationContext(), "check", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
我尝试了上面的三种方法,但都没有起作用,我找不到其他解决方法。
我还尝试了以下代码:
public class PlayerData {
private int gold;
private int wins;
private int losses;
public PlayerData(){
gold = 0;
wins = 0;
losses = 0;
}
Gson gson = new Gson();
String path = this.getApplicationContext().getFilesDir().getAbsolutePath() + "/" + "test.json";
File file = new File(path);
file.setWritable(true);
gson.toJson(new PlayerData(), new FileWriter(file));
但也没有成功。
英文:
Im trying to write into a json file and it just doesnt work, I dont get any errors but nothing changes in the file.
here is the relevant section of my code
try {
//method1
Gson gson = new Gson();
String s = "gjsanhfhj";
String path = this.getApplicationContext().getFilesDir().getAbsolutePath() + "/" + "test.json";
File file = new File(path);
file.setWritable(true);
gson.toJson(s,new FileWriter(file));
//method2
FileOutputStream outputStream;
outputStream = openFileOutput(path, Context.MODE_PRIVATE);
outputStream.write(s.getBytes());
outputStream.close();
//method3
Writer output;
output = new BufferedWriter(new FileWriter(file));
output.write(s);
output.close();
Toast.makeText(this.getApplicationContext(), "check", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
I tried the 3 methods above, didnt work I couldnt find anything else
I tried this
public class PlayerData {
private int gold;
private int wins;
private int losses;
public PlayerData(){
gold = 0;
wins = 0;
losses = 0;
}
Gson gson = new Gson();
String path = this.getApplicationContext().getFilesDir().getAbsolutePath() + "/" + "test.json";
File file = new File(path);
file.setWritable(true);
gson.toJson(new PlayerData(),new FileWriter(file));
didnt work
答案1
得分: 0
Gson.toJson(Obj)用于将对象转换为一个键值对的Json字符串。Gson.toJson()接受一个Java对象作为参数,而您传递的是一个字符串值,这不正确,我认为。例如,Employee类的对象可以转换为以下json。
class Employee {
int empId;
String name;
String department;
}
现在,您可以调用:
gson.toJson(new Employee(100, "abc", "xyz"), new FileWriter(file));
所以,请再次检查您的需求。
英文:
Gson.toJson(Obj) is used to convert an object into a Json-String which is a key value pair. Gson.toJson() takes a java object as an argument, whereas, you are passing a STRING value which not right, I think. For example, an object of Employee class, can be converted into json as below.
class Employee {
int empId;
String name;
String department;
} `
Now, you can call
gson.toJson(new Employee(100, "abc", "xyz"), new FileWriter(file));
So, please check your requirement again.
答案2
得分: 0
以下是翻译好的代码部分:
尝试这个。这对我有效。
private class myString {
String str;
public myString() {
this.str = "Testing123...";
}
}
Gson gson = new Gson();
myString s = new myString();
File file = new File(getApplicationContext().getFilesDir(), "Test.json");
try {
if(!file.exists())
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
byte[] arr = gson.toJson(s).getBytes();
fileOutputStream.write(arr);
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
如果您有任何其他需要,请随时告诉我。
英文:
Try this. This worked for me.
private class myString {
String str;
public myString() {
this.str = "Testing123...";
}
}
Gson gson = new Gson();
myString s = new myString();
File file = new File(getApplicationContext().getFilesDir(), "Test.json");
try {
if(!file.exists())
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
byte[] arr = gson.toJson(s).getBytes();
fileOutputStream.write(arr);
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论