简体   繁体   中英

GSON to JSON - multiple objects

I am using GSON and would like to have it convert multiple lists to JSON - that will be one object - is there any way to do this without manipulating the lists or using a new data structure?

List<String> list1 = new ArrayList<>();
List<String> list2 = new ArrayList<>();

list1.populate();
list2.populate(); 

Gson gson = new GsonBuilder().create(); 
gson.toJson(list1, list2);// ----> THIS IS WHAT I WANT

I want the result to be something like this - pseudocode JSON first result of first list to correlate with first result of second list

{
  list1[element1]: list2[element1], 
  list1[element2]: list2[element2],
  list1[element3]:  list2[element3].....
}

What's wrong with modification of the list in memory?

list1.populate();
list2.populate(); 
list1.addAll(list2); 

Gson gson = new GsonBuilder().create(); 
gson.toJson(list1);

Obviously populate isn't a method of lists, but you can only convert single objects to JSON

If order is at all important, you have to find a way to populate a singular list containing the data in the order you need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM