skip to Main Content

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

2

Answers


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

    Login or Signup to reply.
  2. 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();
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search