GSON将arraylist中的java对象添加到现有的json文件中?

时间:2018-05-18 14:41:30

标签: java json arraylist gson

我有一个程序,当前可以从JSON文件中读取并将它们实现到一个对象中,并存储在对象的arraylist中。我昨天问了一个关于如何回写JSON文件的问题,有人建议使用.put方法但是这不能以JSON格式正确写入。例如,当.json文件中存在如下现有JSON对象时:  [ { "waterSpace": 4.0, "airSpace": 5.0, "landSpace": 3.0, "name": "robbie", "penType": "pen" } ] 但是当我使用.put添加它时,会执行以下 [ { "waterSpace": 4.0, "airSpace": 5.0, "landSpace": 3.0, "name": "robbie", "penType": "pen" } ]{"waterSpace": 4.0, "airSpace": 5.0, "landSpace": 3.0, "name": "robbie", "penType": "pen" }

如何使用GSON正确添加arraylist中的对象,以便格式化正确,如下所示: [ { "waterSpace": 4.0, "airSpace": 5.0, "landSpace": 3.0, "name": "robbie", "penType": "pen" },{ "waterSpace": 4.0, "airSpace": 5.0, "landSpace": 3.0, "name": "robbie", "penType": "pen" } ]

2 个答案:

答案 0 :(得分:0)

GSON也有一个序列化程序,所以你只需要使用fromJson方法将你的对象再次转换为JSON。

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;

public class Deserializer {

    public static void main(String[] args) {
       Gson gson = new Gson();
       List<YourObject> yourObjectList = new ArrayList<>();
       YourObject object1 = new YourObject(10.2, 3.7, 8.5, "test", "test");
       YourObject object2 = new YourObject(5.7, 4.9, 3.1, "test2", "test2");
       yourObjectList.add(object1);
       yourObjectList.add(object2);
       JSONArray jsonArray = new JSONArray(
            gson.toJson(yourObjectList, new TypeToken<List<YourObject>>() {}.getType()));
       System.out.println(jsonArray);
   }

    public static class YourObject {

       private double waterSpace;
       private double airSpace;
       private double landscape;
       private String name;
       private String penType;

       public YourObject(double waterSpace, double airSpace, double landscape, String name, String penType) {
           this.waterSpace = waterSpace;
           this.airSpace = airSpace;
           this.landscape = landscape;
           this.name = name;
           this.penType = penType;
      }

      @Override
      public String toString() {
        return String.valueOf(this.waterSpace) + " " + String.valueOf(this.airSpace) + " "
                + String.valueOf(this.landscape) + " " + this.name + " " + this.penType;
      }
   }
}

答案 1 :(得分:-1)

这是一个在Spring启动项目中实现的示例,但您可以在简单的Java应用程序或其他应用程序中执行此操作:

@SpringBootApplication
public class AlfredValidator1Application  implements CommandLineRunner{

public static void main(String[] args) {
    SpringApplication.run(AlfredValidator1Application.class, args);
}

@Override
public void run(String... args) throws Exception {

    test t1=new test(4.0, 5.0, 3.0, "robbie", "pen");
    test t2=new test(4.0, 5.0, 3.0, "robbie", "pen");
    List<test> listTest=new ArrayList<>();
    listTest.add(t1);
    listTest.add(t2);
    System.out.println(new ObjectMapper().writeValueAsString(listTest));// you can use Gson instead
}
}

class test{
private double waterSpace;
private double airSpace;
private double landSpace;
private String name;
private String penType;

public test(){

}

public test(double waterSpace, double airSpace, double landSpace, String name, String penType) {
    super();
    this.waterSpace = waterSpace;
    this.airSpace = airSpace;
    this.landSpace = landSpace;
    this.name = name;
    this.penType = penType;
}

public double getWaterSpace() {
    return waterSpace;
}

public void setWaterSpace(double waterSpace) {
    this.waterSpace = waterSpace;
}

public double getAirSpace() {
    return airSpace;
}

public void setAirSpace(double airSpace) {
    this.airSpace = airSpace;
}

public double getLandSpace() {
    return landSpace;
}

public void setLandSpace(double landSpace) {
    this.landSpace = landSpace;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPenType() {
    return penType;
}

public void setPenType(String penType) {
    this.penType = penType;
}

}