Json与常见的类型之间的转换

来源:互联网 发布:android高级编程 pdf 编辑:程序博客网 时间:2024/05/04 06:56

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); 
0 0