Json输出List集合对象和map对象 JSON格式
Json输出List集合对象和map对象 JSON格式
//Json输出List集合对象 [{"属性1":["值1"],"属性2":"值2"}, {"属性3":["值3"],"属性4":"值4"}]
代码语言:javascript代码运行次数:0运行复制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"}
代码语言:javascript代码运行次数:0运行复制/**
* {"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));
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2023-09-25,如有侵权请联系 cloudcommunity@tencent 删除集合jsonlistmap对象发布者:admin,转转请注明出处:http://www.yc00.com/web/1754976319a5223572.html
评论列表(0条)