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

猜你喜欢

转载自blog.csdn.net/qq_27954241/article/details/80773938