[英]Convert List to JsonObject in Java using json simple
I have a list - 我有一个清单-
List<Item> items = new ArrayList<Item>;
which consists of items - [firstname , abc , lastname , pqr , id , 1 ] 它由以下各项组成- [firstname,abc,lastname,pqr,id,1]
I need to convert this list to JSONObject of below format in java using json simple lib - 我需要使用json simple lib在Java中将此列表转换为以下格式的JSONObject-
{"firstname":"abc","lastname":"pqr","id":"1"} {“名字”:“ abc”,“姓氏”:“ pqr”,“ id”:“ 1”}
How can I achieve this? 我该如何实现? I am just a beginner.Any help would be appreciated.Thankyou in advance.
我只是一个初学者。任何帮助将不胜感激。谢谢您。
Got the answer - 得到了答案-
First converted the List to Map and then to Json - 首先将List转换为Map,然后转换为Json-
public Map<String, String> test() {
Map<String, String> result = items.stream().collect(Collectors.toMap(Item::getValue, Item::getType)); //Converts List items to Map
System.out.println("Result : " + result);
JSONObject json = new JSONObject(result); //Converts MAP to JsonObject
System.out.println("JSON : " + json); //prints {"firstname":"abc","lastname":"pqr","id":"1"}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.