List> to org.json.JSONObject?

后端 未结 8 2048
借酒劲吻你
借酒劲吻你 2020-12-14 20:12
List> list = new ArrayList>();
Map map = new HashMap();

         


        
8条回答
  •  眼角桃花
    2020-12-14 20:38

    You can do it using both:

    JSONArray directly as,

    String toJson(Collection> list)
    {       
        return new JSONArray(list).toString();
    }
    

    Or by iterating the list with Java8 (like @ShadowJohn solution):

    String toJson(Collection> list)
    {       
        return new JSONArray( 
                list.stream()
                    .map((map) -> new JSONObject(map))
                .collect(Collectors.toList()))
            .toString();
    }
    

提交回复
热议问题