您的位置:首页 > Web前端 > JavaScript

JSON学习(三):Json与常见的类型之间的转换

2017-04-10 15:04 441 查看
list转json
List list=new ArrayList();
list.add("1");
list.add("2");
JsonArray json=JsonArray.fromObject(list);
map转json
Map map = new HashMap();
map.put("欧尼", "json");
map.put("听我", Boolean.TRUE);
map.put("three", new Integer(1));
JSONObject json = JSONObject.fromObject(map);
 Bean转换成json代码
JsonBean jb=new JsonBean()
JSONObject jsonObject = JSONObject.fromObject(jb);
数组转换成json代码
int [] arr = new int[] { 1,2,3};
JSONArray jsonarray = JSONArray.fromObject(arr);
转换成json代码
JSONArray jsonarr = JSONArray.fromObject("['json','is','easy']" );

String jsonStr =   "{\"id\": 2," +
" \"title\": \"json title\", " +
"\"config\": {" +
"\"name\": 34," +
"\"type\": 35," +
"}, \"class\": [" +
"\"one\", \"two\", \"three\"" +
"]}";
//转换成为JSONObject对象
JSONObject jsonObj = new JSONObject(jsonStr);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: