All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anysoft.util.JsonTools Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
package com.anysoft.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.JsonProviderFactory;


/**
 * Json的一些工具
 * @author duanyy
 * @since 1.0.6
 * 
 * @version 1.0.8 [20140410 duanyy] 
* + Add {@link com.anysoft.util.JsonTools#setString(Map, String, String) setString(Map, String, String)}
* + Add {@link com.anysoft.util.JsonTools#setInt(Map, String, int) setInt(Map,String,int)}
* + Add {@link com.anysoft.util.JsonTools#setBoolean(Map, String, boolean) setBoolean(Map,String,boolean)}
* + Add {@link com.anysoft.util.JsonTools#getBoolean(Map, String, boolean) getBoolean(Map,String,boolean)}
* * @version 1.0.14 [20140615 duanyy]
* + 增加一下Json工具 * * @version 1.4.1 [20140901 duanyy]
* + 增加getDouble和setDouble方法 * * @version 1.4.2 [20140903 duanyy]
* + 修正getDouble和getLong的类型转换bug * * @version 1.4.4 [20140912 duanyy]
* + 将Map参数进行参数化
* * @version 1.6.4.27 [20160125 duanyy]
* - 根据sonar建议优化代码
* * @version 1.6.4.41 [20160401 duanyy]
* - 增加float操作方法
* * @version 1.6.6.13 [20170106 duanyy]
* - 增加json2txt和txt2json功能
* * @version 1.6.7.5 [20170119 duanyy]
* - 允许输入的json为空
*/ public class JsonTools { protected static JsonProvider provider = null; static { provider = JsonProviderFactory.createProvider(); } private JsonTools(){ } /** * 从Json对象中获取指定的属性值 * * @param json Json对象 * @param name 属性名 * @param defaultValue 缺省值 * @return 属性值 */ public static String getString(Map json,String name,String defaultValue){ if (json == null){ return defaultValue; } Object found = json.get(name); if (found == null){ return defaultValue; } if (found instanceof String){ return (String)found; } return found.toString(); } /** * 向Json对象中设置指定的属性值 * @param json Json对象 * @param name 属性名 * @param value 属性值(String) * * @since 1.0.8 */ public static void setString(Map json,String name,String value){ if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(value)){ json.put(name, value); } } /** * 从Json对象中获取指定的属性值 * @param json Json对象 * @param name 属性名 * @param defaultValue 缺省值 * @return 属性值 */ public static int getInt(Map json,String name,int defaultValue){ if (json == null){ return defaultValue; } Object found = json.get(name); if (found == null){ return defaultValue; } if (found instanceof Integer){ return (Integer)found; } try { return Integer.parseInt(found.toString()); }catch (NumberFormatException ex){ return defaultValue; } } /** * 向Json对象中设置指定属性值 * @param json * @param name * @param value * * @since 1.4.1 */ public static void setDouble(Map json,String name,double value){ if (StringUtils.isNotEmpty(name)){ json.put(name, value); } } /** * 从Json对象中获取属性值 * @param json * @param name * @param dftValue * @return double value * * @since 1.4.1 */ public static double getDouble(Map json,String name,double dftValue){ if (json == null){ return dftValue; } Object found = json.get(name); if (found == null){ return dftValue; } if (found instanceof Double){ return (Double)found; } try { return Double.parseDouble(found.toString()); }catch (NumberFormatException ex){ return dftValue; } } /** * 向Json对象中设置指定值 * @param json Json对象 * @param name 属性名 * @param value float值 */ public static void setFloat(Map json,String name,float value){ if (StringUtils.isNotEmpty(name)){ json.put(name, value); } } /** * 从Json对象中获取属性值 * @param json Json对象 * @param name 属性名 * @param dftValue 缺省值 * @return float value * * @since 1.6.4.41 */ public static float getFloat(Map json,String name,float dftValue){ if (json == null){ return dftValue; } Object found = json.get(name); if (found == null){ return dftValue; } if (found instanceof Float){ return (Float)found; } try { return Float.parseFloat(found.toString()); }catch (NumberFormatException ex){ return dftValue; } } /** * 向Json对象中设置指定属性值 * @param json Json对象 * @param name 属性名 * @param value 属性值 */ public static void setInt(Map json,String name,int value){ if (StringUtils.isNotEmpty(name)){ json.put(name,value); } } /** * 从Json对象中获取指定的属性值 * @param json Json对象 * @param name 属性名 * @param defaultValue 缺省值 * @return 属性值 * * @since 1.0.14 */ public static long getLong(Map json,String name,long defaultValue){ if (json == null){ return defaultValue; } Object found = json.get(name); if (found == null){ return defaultValue; } if (found instanceof Long){ return (Long)found; } try { return Long.parseLong(found.toString()); }catch (NumberFormatException ex){ return defaultValue; } } /** * 向Json对象中设置指定属性值 * @param json Json对象 * @param name 属性名 * @param value 属性值 * * @since 1.0.14 */ public static void setLong(Map json,String name,long value){ if (StringUtils.isNotEmpty(name)){ json.put(name,value); } } /** * 从Json对象中获取指定的属性值 * @param json Json对象 * @param name 属性名 * @param defaultValue 缺省值 * @return 属性值 * * @since 1.0.8 */ public static boolean getBoolean(Map json,String name,boolean defaultValue){ if (json == null){ return defaultValue; } Object found = json.get(name); if (found == null){ return defaultValue; } if (found instanceof Boolean){ return (Boolean)found; } return BooleanUtils.toBoolean(found.toString()); } /** * 向Json对象中设置指定的属性值 * @param json Json对象 * @param name 属性名 * @param value 属性值 */ public static void setBoolean(Map json,String name,boolean value){ if (StringUtils.isNotEmpty(name)){ json.put(name,value); } } @SuppressWarnings("unchecked") public static void xml2Json(Element root, Map json) { NodeList children = root.getChildNodes(); for (int i = 0, length = children.getLength(); i < length; i++) { Node n = children.item(i); int nodeType = n.getNodeType(); if (nodeType == Node.ELEMENT_NODE){ Element e = (Element) n; String key = e.getNodeName(); List array = null; Object found = json.get(key); if (found != null){ if (found instanceof List){ array = (List)found; }else{ array = new ArrayList(); Object removed = json.remove(key); if (removed != null){ array.add(removed); } json.put(key, array); } } Map map = new HashMap(); //clone attribute { NamedNodeMap attrs = e.getAttributes(); for (int j = 0; j < attrs.getLength(); j++) { Node attr = attrs.item(j); map.put(attr.getNodeName(), attr.getNodeValue()); } } xml2Json(e,map); if (array != null){ array.add(map); }else{ json.put(key, map); } } } } @SuppressWarnings("unchecked") public static void json2Xml(List json,Element e,String key){ Document doc = e.getOwnerDocument(); for (Object item:json){ if (item instanceof Map){ Element newElem = doc.createElement(key); json2Xml((Map)item,newElem); e.appendChild(newElem); }else{ if (item instanceof List){ json2Xml((List)item,e,key); }else{ Element newElem = doc.createElement(key); newElem.appendChild(doc.createTextNode(item.toString())); e.appendChild(newElem); } } } } @SuppressWarnings("unchecked") public static void json2Xml(Map json,Element e){ Set keys = json.keySet(); Iterator iter = keys.iterator(); Document doc = e.getOwnerDocument(); while (iter.hasNext()) { String key = iter.next(); Object data = json.get(key); if (data instanceof List) { List list = (List) data; json2Xml(list, e, key); } else { if (data instanceof Map) { Element newElem = doc.createElement(key); json2Xml((Map) data, newElem); e.appendChild(newElem); continue; } else { Element newElem = doc.createElement(key); newElem.appendChild(doc.createTextNode(data.toString())); e.appendChild(newElem); } } } } @SuppressWarnings("unchecked") public static void clone(List src,List dest){ for (Object item:src){ if (item instanceof Map){ Map newData = new HashMap(); // NOSONAR clone((Map)item,newData); dest.add(newData); }else{ if (item instanceof List){ List newList = new ArrayList(); // NOSONAR clone((List)item,newList); }else{ dest.add(item.toString()); } } } } @SuppressWarnings("unchecked") public static void clone(Map src,Map dest){ Set keys = src.keySet(); Iterator iter = keys.iterator(); while (iter.hasNext()) { String key = iter.next(); Object data = src.get(key); if (data instanceof List) { List list = (List) data; List newList = new ArrayList(); // NOSONAR clone(list, newList); dest.put(key, newList); } else { if (data instanceof Map) { Map newData = new HashMap(); // NOSONAR clone((Map) data, newData); dest.put(key, newData); continue; } else { dest.put(key, data.toString()); } } } } /** * 将Map对象转换为Json文本 * @param json map对象 * @return Json文本 */ public static String map2text(Map json){ return provider.toJson(json); } /** * 将json文本转换为Map对象 * @param text json文本 * @return Map对象 */ @SuppressWarnings("unchecked") public static Map text2map(String text){ Object json = provider.parse(text); if (json instanceof Map){ return (Map)json; } return null; } /** * 将List对象转换为Json文本 * @param json List对象 * @return Json文本 */ public static String list2text(List json){ return provider.toJson(json); } /** * 将Json文本转换为List对象 * @param text Json文本 * @return List对象 */ @SuppressWarnings("unchecked") public static List text2list(String text){ Object json = provider.parse(text); if (json instanceof List){ return (List)json; } return null; } }