首页 > 代码库 > Json与常见的类型之间的转换

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

常用的json

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

 

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