I have a class named Order which contains one string list below
Set<String> items;
and when I convert this to JSON:
ObjectMapper mapperObj = new ObjectMapper();
String JSON = mapperObj.writeValueAsString(order);
System.out.println(JSON);
... I get output like below
"items":[
"xyz",
"aaa"
]
I'm looking for an output something like below
"items":[
{
"result":"xyz"
},
{
"result":"aaa"
}
]
I don't want to create a class separately for a single string.
You can use some API, like Jackson, for creating JSON object and print it into string. First create a json ArrayNode
for your items
. Then for each string in your items
, create an ObjectNode
like this,
ObjectNode node = mapper.createObjectNode();
node.put("result", "xyz");
and add them to the ArrayNode
. Finally you print the JSON object out.
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.