Java 如何使用 GSON 将列表转换为 JSON 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27893342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert List to a JSON Object using GSON?
提问by AKIWEB
I have a List which I need to convert into JSON Object using GSON. My JSON Object has JSON Array in it.
我有一个列表,需要使用 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
}
}
Below is my code in which I need to convert my List to JSON Object which has JSON Array in it -
下面是我的代码,我需要将我的列表转换为其中包含 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 = ??
}
As of now, I only have two items in List - So I need my JSON Object like this -
到目前为止,我在 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
}
]
}
What is the best way to do this?
做这个的最好方式是什么?
采纳答案by Sotirios Delimanolis
If response
in your marshal
method is a DataResponse
, then that's what you should be serializing.
如果response
在您的marshal
方法中是 a DataResponse
,那么这就是您应该序列化的内容。
Gson gson = new Gson();
gson.toJson(response);
That will give you the JSON output you are looking for.
这将为您提供您正在寻找的 JSON 输出。
回答by Rod_Algonquin
There is a sample from google gson documentationon how to actually convert the list to json string:
google gson文档中有一个关于如何将列表实际转换为 json 字符串的示例:
Type listType = new TypeToken<List<String>>() {}.getType();
List<String> target = new LinkedList<String>();
target.add("blah");
Gson gson = new Gson();
String json = gson.toJson(target, listType);
List<String> target2 = gson.fromJson(json, listType);
You need to set the type of list in toJson
method and pass the list object to convert it to json string or vice versa.
您需要在toJson
方法中设置列表的类型并传递列表对象以将其转换为 json 字符串,反之亦然。
回答by Pshemo
Assuming you also want to get json in format
假设您还想获得 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
}
]
}
instead of
代替
{"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}]}
you can use pretty printing. To do so use
你可以使用漂亮的印刷。为此,请使用
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(dataResponse);
回答by Subham Kumar
We can also use another workaround by first creating an array of myObject then convert them into list.
我们还可以使用另一种解决方法,首先创建一个 myObject 数组,然后将它们转换为列表。
final Optional<List<MyObject>> sortInput = Optional.ofNullable(jsonArgument)
.map(jsonArgument -> GSON.toJson(jsonArgument, ArrayList.class))
.map(gson -> GSON.fromJson(gson, MyObject[].class))
.map(myObjectArray -> Arrays.asList(myObjectArray));
Benifits:
好处:
- we are not using reflection here. :)
- 我们在这里没有使用反射。:)