通过转换 gson 将一些特定的表导出为文件

我正在尝试将我的两个房间表导出到 phono 的目录中。我想出的方法是我使用转换我的两个表Gson并将其保存到本地目录中。但问题是我不知道如何将 2 个转换后的JSON-s 合并为一个。


我的代码:


   //for first class

    Gson gson = new Gson();

    ArrayList<String> objStrings1 = new ArrayList<String>();

    for(Clas1 data: datas_1){

        objStrings.add(gson.toJson(data));

    }

    saveLocal(objStrings1)


    //for second class

    Gson gson = new Gson();

    ArrayList<String> objStrings2 = new ArrayList<String>();

    for(Clas2 data: datas_2){

        objStrings.add(gson.toJson(data));

    }

    saveLocal(objStrings2)


临摹微笑
浏览 131回答 1
1回答

浮云间

您可以创建一个根Pojo并将这两个列表绑定在一个类中。JSON此外,它比为每个实例生成并稍后连接它要快。请参阅下面的示例,该示例显示了该想法:import com.google.gson.Gson;import com.google.gson.GsonBuilder;import org.apache.commons.lang3.RandomStringUtils;import java.util.Arrays;import java.util.List;import java.util.concurrent.ThreadLocalRandom;public class GsonApp {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; List<Class1> class1s = Arrays.asList(new Class1(), new Class1(), new Class1());&nbsp; &nbsp; &nbsp; &nbsp; List<Class2> class2s = Arrays.asList(new Class2(), new Class2(), new Class2());&nbsp; &nbsp; &nbsp; &nbsp; Pojo root = new Pojo();&nbsp; &nbsp; &nbsp; &nbsp; root.setClass1s(class1s);&nbsp; &nbsp; &nbsp; &nbsp; root.setClass2s(class2s);&nbsp; &nbsp; &nbsp; &nbsp; Gson gson = new GsonBuilder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setPrettyPrinting()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .create();&nbsp; &nbsp; &nbsp; &nbsp; String json = gson.toJson(root);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(json);&nbsp; &nbsp; &nbsp; &nbsp; Pojo deserialised = gson.fromJson(json, Pojo.class);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(deserialised);&nbsp; &nbsp; }}class Pojo {&nbsp; &nbsp; private List<Class1> class1s;&nbsp; &nbsp; private List<Class2> class2s;&nbsp; &nbsp; // getters, setters, toString}class Class1 {&nbsp; &nbsp; private int id = ThreadLocalRandom.current().nextInt(100);&nbsp; &nbsp; // getters, setters, toString}class Class2 {&nbsp; &nbsp; private int age = ThreadLocalRandom.current().nextInt(70);&nbsp; &nbsp; private String name = RandomStringUtils.randomAlphanumeric(5);&nbsp; &nbsp; // getters, setters, toString}上面的代码打印:{&nbsp; "class1s": [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "id": 2&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "id": 1&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "id": 3&nbsp; &nbsp; }&nbsp; ],&nbsp; "class2s": [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "age": 12,&nbsp; &nbsp; &nbsp; "name": "qXcaT"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "age": 33,&nbsp; &nbsp; &nbsp; "name": "3wXv4"&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "age": 18,&nbsp; &nbsp; &nbsp; "name": "55XZ6"&nbsp; &nbsp; }&nbsp; ]}波霍斯:Pojo{class1s=[Class1{id=2}, Class1{id=1}, Class1{id=3}], class2s=[Class2{age=12, name='qXcaT'}, Class2{age=33, name='3wXv4'}, Class2{age=18, name='55XZ6'}]}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java