需要引入的jar包:
1.commons-lang.jar
2.commons-beanutils.jar
3.commons-collections.jar
4.commons-logging.jar
5.ezmorph.jar
6.json-lib-2.2.2-jdk15.jar
具體代碼如下:
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.List;
- import java.util.Map;
- import net.sf.json.JSONObject;
- import org.dom4j.Document;
- import org.dom4j.DocumentException;
- import org.dom4j.DocumentHelper;
- public class JsonTest {
- public static void main(String[] args) throws DocumentException {
- //*******************Map 轉 JSon**********************
- Document doc = DocumentHelper.parseText(getContent(
- "C:\\Documents and Settings\\wzhe\\桌面\\測試用例\\安防\\", "413.txt"));
- Map map = XmlUtils.dom2Map(doc);
- JSONObject jsobj= JSONObject.fromObject(map);
- System.out.println(jsobj.toString());
- //*******************JSON 轉 Map**********************
- Map ttMap= ((Map)JSONObject.fromObject(getContent("C:\\Documents and Settings\\wzhe\\桌面\\測試用例\\JSON\\", "test.txt")));
- Map tmpObj = (Map) ttMap.get("BCR");
- List tmpList= (List) tmpObj.get("BC_INFO");
- Map bcMap = (Map) tmpList.get(0);
- System.out.println(bcMap.get("FAULT_REASON").toString());
- }
- public static String getContent(String filePath, String fileName) {
- File file = new File(filePath, fileName);
- InputStream strm = null;
- StringBuffer content = new StringBuffer();
- try {
- strm = new FileInputStream(file);
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- strm, "GBK"));
- String line = "";
- while ((line = reader.readLine()) != null) {
- content.append(line);
- }
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- return content.toString();
- }
- }
XmlUtils.java
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import org.dom4j.Document;
- import org.dom4j.DocumentException;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import org.dom4j.tree.ContentListFacade;
- import org.dom4j.tree.DefaultAttribute;
- public class XmlUtils {
- public static Map<String, Object> dom2Map(Document doc) {
- Map<String, Object> map = new HashMap<String, Object>();
- // 判斷需要解析的文檔是否爲空
- if (doc == null) {
- return map;
- }
- // 獲取根節點
- Element root = doc.getRootElement();
- // 獲取根節點下的子節點迭代器
- Iterator iterator = root.elementIterator();
- // 循環子節點,開始向map中存值
- while (iterator.hasNext()) {
- Element e = (Element) iterator.next();
- List list = e.elements();
- // 判斷當前節點是否有子節點
- // 如果存在子節點調用element2Map(Element e)方法,不存在子節點直接存進map中
- if (list.size() > 0) {
- map.put(e.getName(), element2Map(e));
- } else {
- saveAttribute2Map(map, e);
- map.put(e.getName(), e.getText());
- }
- }
- return map;
- }
- private static Map<String, Object> element2Map(Element e) {
- Map<String, Object> map = new HashMap<String, Object>();
- List<?> list = e.elements();
- saveAttribute2Map(map, e);
- if (list.size() > 0) {
- for (int i = 0, j = list.size(); i < j; i++) {
- Element iter = (Element) list.get(i);
- List<Object> mapList = new ArrayList<Object>();
- // 存在子節點
- if (iter.elements().size() > 0) {
- Map<?, ?> m = element2Map(iter);
- if (map.get(iter.getName()) != null) {
- Object obj = map.get(iter.getName());
- if (!obj.getClass().getName()
- .equals("java.util.ArrayList")) {
- mapList = new ArrayList<Object>();
- mapList.add(obj);
- mapList.add(m);
- }
- if (obj.getClass().getName()
- .equals("java.util.ArrayList")) {
- mapList = (List) obj;
- mapList.add(m);
- }
- map.put(iter.getName(), mapList);
- } else
- map.put(iter.getName(), m);
- } else {
- if (map.get(iter.getName()) != null) {
- Object obj = map.get(iter.getName());
- if (!obj.getClass().getName()
- .equals("java.util.ArrayList")) {
- mapList = new ArrayList();
- mapList.add(obj);
- mapList.add(iter.getText());
- }
- if (obj.getClass().getName()
- .equals("java.util.ArrayList")) {
- mapList = (List) obj;
- mapList.add(iter.getText());
- }
- map.put(iter.getName(), mapList);
- } else
- map.put(iter.getName(), iter.getText());
- }
- }
- } else {
- saveAttribute2Map(map, e);
- map.put(e.getName(), e.getText());
- }
- return map;
- }
- private static void saveAttribute2Map(Map<String, Object> map, Element e) {
- ContentListFacade attributes = (ContentListFacade) e.attributes();
- if (attributes.size() > 0) {
- HashMap<String, String> attrMap = new HashMap<String, String>();
- map.put("attribute", attrMap);
- DefaultAttribute attrTmp = null;
- for (int i = 0, j = attributes.size(); i < j; i++) {
- attrTmp = (DefaultAttribute) attributes.get(i);
- attrMap.put(attrTmp.getName(), attrTmp.getValue());
- }
- }
- }
- public static String getContent(String filePath, String fileName) {
- File file = new File(filePath, fileName);
- InputStream strm = null;
- StringBuffer content = new StringBuffer();
- try {
- strm = new FileInputStream(file);
- BufferedReader reader = new BufferedReader(new InputStreamReader(
- strm, "GBK"));
- String line = "";
- while ((line = reader.readLine()) != null) {
- content.append(line);
- }
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- return content.toString();
- }
- public static void main(String[] args) throws DocumentException,
- UnsupportedEncodingException {
- Document doc = DocumentHelper.parseText(getContent(
- "C:\\Documents and Settings\\wzhe\\桌面\\測試用例\\安防\\", "413.txt"));
- Map map = XmlUtils.dom2Map(doc);
- Map bcrMap = (Map) map.get("BCR");
- System.out.println(((List) bcrMap.get("BC_INFO")).toArray().toString());
- System.out.println(map.toString());
- }
- }