JSON及XML字符串与Map、List的互相转换

 JSON字符串与Map、List的互相转换

import java.util.List;
import java.util.Map;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
 
public class JsonUtil {
 
    public static void main(String[] args) throws Exception {
        String s = "{\"msg\": \"success\", \"data\": [{\"xm\": \"张三\", \"birthdate\": \"1990-01-18 11:10:41\"},{\"xm\": \"李四\", \"birthdate\": \"1991-01-18 11:10:41\"}]}";
        //json字符串转Map
        Map<String,Object> jsonToMap = JSONObject.parseObject(s);
        System.out.println("jsonToMap:"+jsonToMap);
        //Map转json字符串
        String mapToJson = JSON.toJSONString(jsonToMap);
        System.out.println("mapToJson:"+mapToJson);
        //json字符串转List
        List<Map> jsonToList = JSONArray.parseArray(jsonToMap.get("data").toString(),Map.class);
        System.out.println("jsonToList:"+jsonToList);
        //List转json字符串
        String listToJson = JSON.toJSONString(jsonToList);
        System.out.println("listToJson:"+listToJson);
    }
}

JSON字符串转Map、List依赖JAR包

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.72</version>
</dependency>

XML字符串与Map、List的互相转换

import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class XmlUtil {
 
    /**
     * Map 转为 Xml string
     */
    public static String mapToXml(Map<String, Object> map) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("<DataInfo>");
        buffer.append(convert(map));
        buffer.append("</DataInfo>");
        return buffer.toString();
    }
 
    @SuppressWarnings("unchecked")
    private static String convert(Map<String, Object> map) {
        StringBuffer buffer = new StringBuffer();
        for (String key : map.keySet()) {
            // 拼接xml开头标签
            buffer.append("<" + key + ">");
            if (map.get(key) instanceof Map) {//值为Map类型
                String element = convert((Map<String, Object>) map.get(key));
                buffer.append(element);
            } else if (map.get(key) instanceof List) {//值为List类型
                List<Map<String,Object>> list = (ArrayList<Map<String,Object>>) map.get(key);
                for (int i = 0; i < list.size(); i++) {
                    buffer.append("<entry>");
                    Map<String,Object> m = (HashMap<String,Object>) list.get(i);
                    String element = convert((Map<String, Object>)m);
                    buffer.append(element);
                    buffer.append("</entry>");
                }
            } else {
                buffer.append(map.get(key));
            }
            // 收尾标签
            buffer.append("</" + key + ">");
        }
        return buffer.toString();
    }
 
    /**
     * Xml string 转为 Map
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> xmlToMap(String str) {
        Map<String, Object> result = new HashMap<>();
        try {
            Element root = DocumentHelper.parseText(str).getRootElement();
            // 获取所有子节点
            List<Element> elements = root.elements();
            for (Element element : elements) {
                result.put(element.getName(), element.getText());
            }
            return result;
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * Xml string 转为 List
     */
    @SuppressWarnings("unchecked")
    public static List<Map<String, Object>> xmlToList(String str) {
        List<Map<String, Object>> result = new ArrayList<>();
        try {
            Element root = DocumentHelper.parseText(str).getRootElement();
            // 获取所有子节点
            List<Element> elements = root.elements();
            for (Element element : elements) {
                List<Element> childElements = element.elements();
                Map<String, Object> map = new HashMap<>();
                for(Element childElement : childElements){
                    map.put(childElement.getName(), childElement.getText());
                }
                result.add(map);
            }
            return result;
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * Xml string 转为 Map(其中包含List)
     */
    @SuppressWarnings("unchecked")
    public static Map<String,Object> xmlToMapList(String str) {
        Map<String,Object> result = new HashMap<>();
        try {
            Element root = DocumentHelper.parseText(str).getRootElement();
            // 获取所有子节点
            List<Element> elements = root.elements();
            for (Element element : elements) {
                //若子节点还包含子节点,则转为List集合,否则转为Map集合
                List<Element> childNodes = element.elements();              
                if(childNodes != null && childNodes.size() > 0 && childNodes.get(0).elements().size() > 0){
                    List<Map<String,Object>> reList = xmlToList(element.asXML());
                    result.put(element.getName(),reList);
                }else{
                    Map<String,Object> map = xmlToMap(element.asXML());
                    result.put(element.getName(), map);
                }
            }          
            return result;
        } catch (DocumentException e) {
            e.printStackTrace();
        }       
        return null;
    }
 
    public static void main(String[] args) throws Exception {
        Map<String,Object> headMap = new HashMap<>();
        headMap.put("creator","API");
        headMap.put("token","123456");
        List<Map<String,Object>> dataList = new ArrayList<>();
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("xm","张三");
        dataMap.put("birthdate","1990-01-18 11:10:41");
        dataList.add(dataMap);
        dataMap = new HashMap<>();
        dataMap.put("xm","李四");
        dataMap.put("birthdate","1991-01-18 11:10:41");
        dataList.add(dataMap);
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("DataHead",headMap);
        paramMap.put("DataEntries",dataList);
        //Map转xml字符串
        String xmlStr = mapToXml(paramMap);
        System.out.println("Map转xml字符串:"+xmlStr);
        //xml字符串转Map
        Map<String, Object> map = xmlToMapList(xmlStr);
        System.out.println("xml字符串转Map:"+map);
    }
    
}

XML字符串转Map、List依赖JAR包

<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>

 

posted @ 2021-04-20 13:13  YoungDeng  阅读(620)  评论(0编辑  收藏  举报