简体   繁体   English

GSON到JSON-多个对象

[英]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? 我正在使用GSON,并希望将多个列表转换为JSON(这将是一个对象),有没有办法做到这一点而无需操作列表或使用新的数据结构?

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 我希望结果是这样的-第一个列表的伪代码JSON第一个结果与第二个列表的第一个结果相关

{
  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 显然,填充不是列表的方法,但是您只能将单个对象转换为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 如果顺序非常重要,则必须找到一种方法来填充包含所需顺序的数据的单数列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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