【发布时间】:2015-03-09 17:00:52
【问题描述】:
我有一个需要使用 GSON 将其转换为 JSON 对象的列表。我的 JSON 对象中有 JSON 数组。
public class DataResponse {
private List<ClientResponse> apps;
// getters and setters
public static class ClientResponse {
private double mean;
private double deviation;
private int code;
private String pack;
private int version;
// getters and setters
}
}
下面是我的代码,我需要将我的列表转换为其中包含 JSON 数组的 JSON 对象 -
public void marshal(Object response) {
List<DataResponse.ClientResponse> clientResponse = ((DataResponse) response).getClientResponse();
// now how do I convert clientResponse list to JSON Object which has JSON Array in it using GSON?
// String jsonObject = ??
}
到目前为止,我在 List 中只有两个项目 - 所以我需要这样的 JSON 对象 -
{
"apps":[
{
"mean":1.2,
"deviation":1.3
"code":100,
"pack":"hello",
"version":1
},
{
"mean":1.5,
"deviation":1.1
"code":200,
"pack":"world",
"version":2
}
]
}
最好的方法是什么?
【问题讨论】:
-
您显示的 JSON 不是 JSON 数组,而是 JSON 对象。
-
哎呀抱歉,是的,它是 JSON 对象。让我重新表述一下这个问题。