在安卓中写入JSON文件时出现问题。

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

Issue writing to json file in android

问题

我尝试写入一个 JSON 文件,但它就是不起作用,我没有收到任何错误消息,但文件中什么也没有改变。

以下是我的代码的相关部分:

  1. try {
  2. // 方法1
  3. Gson gson = new Gson();
  4. String s = "gjsanhfhj";
  5. String path = this.getApplicationContext().getFilesDir().getAbsolutePath() + "/" + "test.json";
  6. File file = new File(path);
  7. file.setWritable(true);
  8. gson.toJson(s, new FileWriter(file));
  9. // 方法2
  10. FileOutputStream outputStream;
  11. outputStream = openFileOutput(path, Context.MODE_PRIVATE);
  12. outputStream.write(s.getBytes());
  13. outputStream.close();
  14. // 方法3
  15. Writer output;
  16. output = new BufferedWriter(new FileWriter(file));
  17. output.write(s);
  18. output.close();
  19. Toast.makeText(this.getApplicationContext(), "check", Toast.LENGTH_SHORT).show();
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }

我尝试了上面的三种方法,但都没有起作用,我找不到其他解决方法。

我还尝试了以下代码:

  1. public class PlayerData {
  2. private int gold;
  3. private int wins;
  4. private int losses;
  5. public PlayerData(){
  6. gold = 0;
  7. wins = 0;
  8. losses = 0;
  9. }
  10. Gson gson = new Gson();
  11. String path = this.getApplicationContext().getFilesDir().getAbsolutePath() + "/" + "test.json";
  12. File file = new File(path);
  13. file.setWritable(true);
  14. 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

  1. try {
  2. //method1
  3. Gson gson = new Gson();
  4. String s = "gjsanhfhj";
  5. String path = this.getApplicationContext().getFilesDir().getAbsolutePath() + "/" + "test.json";
  6. File file = new File(path);
  7. file.setWritable(true);
  8. gson.toJson(s,new FileWriter(file));
  9. //method2
  10. FileOutputStream outputStream;
  11. outputStream = openFileOutput(path, Context.MODE_PRIVATE);
  12. outputStream.write(s.getBytes());
  13. outputStream.close();
  14. //method3
  15. Writer output;
  16. output = new BufferedWriter(new FileWriter(file));
  17. output.write(s);
  18. output.close();
  19. Toast.makeText(this.getApplicationContext(), "check", Toast.LENGTH_SHORT).show();
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }

I tried the 3 methods above, didnt work I couldnt find anything else

I tried this

  1. public class PlayerData {
  2. private int gold;
  3. private int wins;
  4. private int losses;
  5. public PlayerData(){
  6. gold = 0;
  7. wins = 0;
  8. losses = 0;
  9. }
  10. Gson gson = new Gson();
  11. String path = this.getApplicationContext().getFilesDir().getAbsolutePath() + "/" + "test.json";
  12. File file = new File(path);
  13. file.setWritable(true);
  14. gson.toJson(new PlayerData(),new FileWriter(file));

didnt work

答案1

得分: 0

Gson.toJson(Obj)用于将对象转换为一个键值对的Json字符串。Gson.toJson()接受一个Java对象作为参数,而您传递的是一个字符串值,这不正确,我认为。例如,Employee类的对象可以转换为以下json。

  1. class Employee {
  2. int empId;
  3. String name;
  4. String department;
  5. }

现在,您可以调用:

  1. 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.

  1. class Employee {
  2. int empId;
  3. String name;
  4. String department;

} `

Now, you can call

  1. gson.toJson(new Employee(100, "abc", "xyz"), new FileWriter(file));

So, please check your requirement again.

答案2

得分: 0

以下是翻译好的代码部分:

  1. 尝试这个这对我有效
  2. private class myString {
  3. String str;
  4. public myString() {
  5. this.str = "Testing123...";
  6. }
  7. }
  8. Gson gson = new Gson();
  9. myString s = new myString();
  10. File file = new File(getApplicationContext().getFilesDir(), "Test.json");
  11. try {
  12. if(!file.exists())
  13. file.createNewFile();
  14. FileOutputStream fileOutputStream = new FileOutputStream(file);
  15. byte[] arr = gson.toJson(s).getBytes();
  16. fileOutputStream.write(arr);
  17. fileOutputStream.close();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }

如果您有任何其他需要,请随时告诉我。

英文:

Try this. This worked for me.

  1. private class myString {
  2. String str;
  3. public myString() {
  4. this.str = "Testing123...";
  5. }
  6. }
  7. Gson gson = new Gson();
  8. myString s = new myString();
  9. File file = new File(getApplicationContext().getFilesDir(), "Test.json");
  10. try {
  11. if(!file.exists())
  12. file.createNewFile();
  13. FileOutputStream fileOutputStream = new FileOutputStream(file);
  14. byte[] arr = gson.toJson(s).getBytes();
  15. fileOutputStream.write(arr);
  16. fileOutputStream.close();
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20. }

huangapple
  • 本文由 发表于 2023年4月20日 08:04:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059630.html
匿名

发表评论

匿名网友

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

确定