标签:json 用户 gets san ref fas fast out 对象
//String to Json String str = "{\"语文\":\"88\",\"数学\":\"78\",\"计算机\":\"99\"}"; JSONObject jsonObject; jsonObject = JSONObject.parseObject(str); System.out.println("jsonObject: "+jsonObject); // Json to String String chinessName = jsonObject.getString("语文"); System.out.println("chinessName: "+chinessName); //对象转json字符串简单,就一个toJSONString(对象);方法 User user = new User(); user.setName("zhangsan"); user.setPassword("123"); user.setAge(18); String string = JSON.toJSONString(user); System.out.println("user.toString(): "+user.toString()); System.out.println("JSON.toJSONString(user): "+string); System.out.println(".................................................."); //List的json字符串转会list对象,只需要使用parseArray(str,类名.class); User user1 = new User(); user1.setName("lisi"); user1.setPassword("321"); user1.setAge(17); List<User> users = new ArrayList<User>(); users.add(user); users.add(user1); String string2 = JSON.toJSONString(users); System.out.println("user list"+string2); List<User> list = JSON.parseArray(string2, User.class); for (User user2 : list) { System.out.println(user2); } System.out.println(".................................................."); //Map的json字符串转为map对象,使用parseObject(str,new TypeReference<Map<泛型,泛型>>(){}); Map<String,User> map = new HashMap<String,User>(); map.put("用户1", user); map.put("用户2", user1); String string3 = JSON.toJSONString(map); System.out.println(string3);
标签:json 用户 gets san ref fas fast out 对象
原文地址:https://www.cnblogs.com/zhougongjin/p/12093326.html