我們需要先把json字串轉化為
net.sf.json.JSONObject
1.將陣列轉換為JSON:String[] arr = {"asd","dfgd","asd","234"};JSONArray jsonarray = JSONArray.fromObject(arr);System.out.println(jsonarray);
2.物件轉換成JSON:UserInfo user = new UserInfo(1001,"張三");JSONArray jsonArray = JSONArray.fromObject(user);System.out.println( jsonArray );
3.把Map轉換成json, 要使用jsonObject物件:Map<String, Object> map = new HashMap<String, Object>();map.put("userId", 1001);map.put("userName", "張三");map.put("userSex", "男");JSONObject jsonObject = JSONObject.fromObject(map);System.out.println(jsonObject);
我們需要先把json字串轉化為
net.sf.json.JSONObject
物件,java中這樣就可以完成json字串到Map的轉換了。1.將陣列轉換為JSON:String[] arr = {"asd","dfgd","asd","234"};JSONArray jsonarray = JSONArray.fromObject(arr);System.out.println(jsonarray);
2.物件轉換成JSON:UserInfo user = new UserInfo(1001,"張三");JSONArray jsonArray = JSONArray.fromObject(user);System.out.println( jsonArray );
3.把Map轉換成json, 要使用jsonObject物件:Map<String, Object> map = new HashMap<String, Object>();map.put("userId", 1001);map.put("userName", "張三");map.put("userSex", "男");JSONObject jsonObject = JSONObject.fromObject(map);System.out.println(jsonObject);