This article describes the JSONUtil tool class and usage of Java implementation. Share it for your reference, as follows:
import java.util.HashMap;import java.util.Map;import com.alibaba.druid.util.StringUtils;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;public class JSONUtils { /** * Bean object to JSON * * @param object * @param dataFormatString * @return */ public static String beanToJson(Object object, String dataFormatString) { if (object != null) { if (StringUtils.isEmpty(dataFormatString)) { return JSONObject.toJSONString(object); } return JSON.toJSONStringWithDateFormat(object, dataFormatString); } else { return null; } } /** * Bean object to JSON * * @param object * @return */ public static String beanToJson(Object object) { if (object != null) { return JSON.toJSONString(object); } else { return null; } } /** * String to JSON string* * @param key * @param value * @return */ public static String stringToJsonByFastjson(String key, String value) { if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value)) { return null; } Map<String, String> map = new HashMap<String, String>(); map.put(key, value); return beanToJson(map, null); } /** * Convert json string to object* * @param json * @param clazz * @return */ public static Object jsonToBean(String json, Object clazz) { if (StringUtils.isEmpty(json) || clazz == null) { return null; } return JSON.parseObject(json, clazz.getClass()); } /** * json string to map * * @param json * @return */ @SuppressWarnings("unchecked") public static Map<String, Object> jsonToMap(String json) { if (StringUtils.isEmpty(json)) { return null; } return JSON.parseObject(json, Map.class); }}test:
JSON string to map
public static void main(String[] args) { String jsonStr = "{'userName':'huangbaokang','password':'123456'}"; Map<String, Object> map = JSONUtils.jsonToMap(jsonStr); System.out.println(map.get("userName"));}Output:
huangbaokang
JSON string to object:
Create a new User class
public class User { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}public static void main(String[] args) { String jsonStr = "{'userName':'huangbaokang','password':'123456'}"; User user = (User) JSONUtils.jsonToBean(jsonStr, new User()); System.out.println("Username="+user.getUserName()+" Password="+user.getPassword());}Test output:
Output username =huangbaokang Password =123456
Object to JSON
public static void main(String[] args) { User user = new User(); user.setUserName("huangbaokang"); user.setPassword("123456"); String result = JSONUtils.beanToJson(user); System.out.println(result);}Test output:
{"password":"123456","userName":"huangbaokang"}
The jar applied here is Alibaba's fastjson, and the relevant jar is added to the lib.
PS: Regarding json operation, here are some practical json online tools for your reference:
Online JSON code verification, inspection, beautification and formatting tools:
http://tools.VeVB.COM/code/json
JSON online formatting tool:
http://tools.VeVB.COM/code/jsonformat
Online XML/JSON mutual conversion tool:
http://tools.VeVB.COM/code/xmljson
json code online formatting/beautification/compression/editing/converting tools:
http://tools.VeVB.COM/code/jsoncodeformat
Online json compression/escaping tools:
http://tools.VeVB.COM/code/json_yasuo_trans
For more information about Java related content, please check out the topics of this site: "Summary of Java operation json format data skills", "Summary of Java array operation skills", "Summary of Java characters and string operation skills", "Summary of Java mathematical operation skills", "Tutorial of Java Data Structure and Algorithm" and "Summary of Java operation DOM node skills"
I hope this article will be helpful to everyone's Java programming.