read and write data with GSON

后端 未结 5 730
予麋鹿
予麋鹿 2021-01-11 21:23

I am struggling to find a good example on how to read and write data in my android app using GSON. Could someone please show me or point me to a good example? I am using thi

5条回答
  •  生来不讨喜
    2021-01-11 22:02

    Simple Gson example:

    public class Main {
    
        public class Power {
            private String name;
            private Long damage;
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public Long getDamage() {
                return damage;
            }
    
            public void setDamage(Long damage) {
                this.damage = damage;
            }
    
            public Power() {
                super();
            }
    
            public Power(String name, Long damage) {
                super();
                this.name = name;
                this.damage = damage;
            }
    
            @Override
            public String toString() {
                return "Power [name=" + name + ", damage=" + damage + "]";
            }
    
        }
    
        public class Warrior {
            private String name;
            private Power power;
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public Power getPower() {
                return power;
            }
    
            public void setPower(Power power) {
                this.power = power;
            }
    
            public Warrior() {
                super();
            }
    
            public Warrior(String name, Power power) {
                super();
                this.name = name;
                this.power = power;
            }
    
            @Override
            public String toString() {
                return "Warrior [name=" + name + ", power=" + power.toString() + "]";
            }
    
        }
    
        public static void main(String[] args) {
            Main m = new Main();
            m.run();
        }
    
        private void run() {
            Warrior jake = new Warrior("Jake the dog", new Power("Rubber hand", 123l));
            String jsonJake = new Gson().toJson(jake);
            System.out.println("Json:"+jsonJake);
            Warrior returnToWarrior = new Gson().fromJson(jsonJake, Warrior.class);
            System.out.println("Object:"+returnToWarrior.toString());
        }
    
    }
    

    Anyways checkout the documentation.

    And to persist something in your application you can start with something simple like ORMlite.

    Hope this help! :]

    UPDATE:

    If you really want write the json in a file:

            File myFile = new File("/sdcard/myjsonstuff.txt");
            myFile.createNewFile();
            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter =new OutputStreamWriter(fOut);
            myOutWriter.append(myJsonString);
            myOutWriter.close();
            fOut.close();
    

    And if you want to read:

            File myFile = new File("/sdcard/myjsonstuff.txt");
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
            String aDataRow = "";
            String aBuffer = ""; //Holds the text
            while ((aDataRow = myReader.readLine()) != null) 
            {
                aBuffer += aDataRow ;
            }
            myReader.close();
    

    Also add: to your manifest.

    But, seriously is so much better use a ORM and store the records in the db. I don't know why you need save the json data in a file, but if I was you, I will use the ORM way.

提交回复
热议问题