Json输出List集合对象和map对象 JSON格式
Json输出List集合对象和map对象 JSON格式
//Json输出List集合对象 [{"属性1":["值1"],"属性2":"值2"}, {"属性3":["值3"],"属性4":"值4"}]
import com.alibaba.fastjson.JSONObject; import java.util.ArrayList; import java.util.List; public class PhotoVo { /** * 图片路径列表 */ private List<String> pathList; /** * 图片类型 */ private Integer type; public List<String> getPathList() { return pathList; } public void setPathList(List<String> pathList) { this.pathList = pathList; } public Integer getType() { return type; } public void setType(Integer type) { this.type = type; } /** * json=[{"pathList":["/aaa.jpg"],"type":1},{"pathList":["/bbb.jpg"],"type":2}] * @param args */ public static void main(String[] args) { List<PhotoVo> list = new ArrayList<PhotoVo>(); PhotoVo vo = new PhotoVo(); vo.setType(1); List<String> path = new ArrayList<String>(); path.add("/aaa.jpg"); vo.setPathList(path); list.add(vo); vo = new PhotoVo(); vo.setType(2); path = new ArrayList<String>(); path.add("/bbb.jpg"); vo.setPathList(path); list.add(vo); System.out.println("json=" + JSONObject.toJSONString(list)); } }
//Json输出map对象 {"键1":"值1","键2":"值2"}
/** * {"aaa":"bbb","111":"222"} * @param args */ public static void main(String[] args) { Map<String,String> map = new HashMap<>(); map.put("111","222"); map.put("aaa","bbb"); System.out.println(JSON.toJSONString(map)); }