通过解析xml获得json格式,动态解析json
来源:互联网 发布:csgo 优化 编辑:程序博客网 时间:2024/06/15 23:24
前几天,公司要实现接口api开放平台,因为涉及到多个关联方。如果用bean存储每个关联方的接口。那会写无数个bean。自己研究了下。实现了一个通过配置xml的对应json格式的文件,然后读取,进行解析json数据。吧对应的数据存到数据库,要用的时候再进行取值。
贴上测试数据
xml文件
<?xml version="1.0" encoding="UTF-8"?> <JSONFORMAT> <student type = "list"> <name type = "single"></name> <age type = "single"></age> <score type = "single"></score><sex type = "single"></sex> </student> <chiness type = "map"> <shiju type = "single"></shiju> <yuedu type = "single"></yuedu> <zuowen type = "single"></zuowen> </chiness> <match type = "single"></match> </JSONFORMAT>
对应要解析的json数据
{ "student":[ { "score":"98", "sex":"女", "name":"张三", "age":"13" }, { "score":"98a", "sex":"女a", "name":"张三a", "age":"13a" } ], "chiness":{ "yuedu":"90", "zuowen":"80", "shiju":"100" }, "match":"88"}
处理通过解析xml文件获得需要解析的参数
ContractUtil.java
import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class ContractUtil {public static ArrayList<Map<String,Object>> arraylist = new ArrayList<>(); // 属性容器public static Document read(String fileName) throws Exception {SAXReader reader = new SAXReader();String path = ClassPathUtil.getClassPath(AutoJavaBean.class);Document document = reader.read(new File(path + "//" + fileName));System.out.println("配置文件路径:" + path + "//" + fileName);return document;}public static void getInterfaceParam(Element root, int level,String topName) {for (Iterator i = root.elementIterator(); i.hasNext();) {Element el = (Element) i.next();Map<String, Object> maps = new HashMap<>();maps.put("name", el.getName());maps.put("level", level);maps.put("topName", topName);maps.put("isMap", "map".equals(el.attributeValue("type"))?"Y":"N");maps.put("isList", "list".equals(el.attributeValue("type"))?"Y":"N");arraylist.add(maps);if("map".equals(el.attributeValue("type"))||"list".equals(el.attributeValue("type"))){getInterfaceParam(el, level + 1,el.getName());}}}public static void main(String[] args) throws DocumentException {String fileName = "Contract.xml";SAXReader reader = new SAXReader();String path = ClassPathUtil.getClassPath(AutoJavaBean.class);Document document = reader.read(new File(path + "//" + fileName));Element root = document.getRootElement();ContractUtil.getInterfaceParam(root,0,"");}}
针对报文进行解析处理类
XmlDemo.java
import java.util.ArrayList;import java.util.Map;import org.dom4j.Document;import org.dom4j.Element;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;public class XmlDemo {public static void main(String[] args) throws Exception {// String json = "{\"returnCode\":\"0000\",\"repayPlan\":[{\"loanNo\":\"1111\",\"payprincipalamt\":20000.0,\"feePlan\":[{\"feeCode\":\"CORPUS\",\"payfeeamt\":220.0}],\"payinteamt\":0.0,\"termAmt\":20000.0,\"termNo\":1,\"loanRate\":4.5,\"payDate\":\"20170427\"}]}";//String json1 = "{\"returnCode\":\"0000\",\"repayPlan\":[{\"loanNo\":\"1111\",\"payprincipalamt\":20000,\"feePlan\":[{\"feeCode\":\"CORPUS\",\"payfeeamt\":220},{\"feeCode\":\"AAAAA\",\"payfeeamt\":3330}],\"payinteamt\":0,\"termAmt\":20000,\"termNo\":1,\"loanRate\":4.5,\"payDate\":\"20170427\"}]}";//matchJson(json1);JSONObject obj = new JSONObject();obj.put("name", "张三");obj.put("age", "13");obj.put("score", "98");obj.put("sex", "女");JSONObject obja = new JSONObject();obja.put("name", "张三a");obja.put("age", "13a");obja.put("score", "98a");obja.put("sex", "女a");JSONArray obj2 = new JSONArray();obj2.add(obj);obj2.add(obja);JSONObject obja2 = new JSONObject();obja2.put("shiju", "100");obja2.put("yuedu", "90");obja2.put("zuowen", "80");JSONObject obj1 = new JSONObject();obj1.put("student", obj2);obj1.put("chiness", obja2);obj1.put("match", "88");System.out.println(obj1);matchJson(obj1.toJSONString());}public static void matchJson(String json) throws Exception {//String fileName = "Contract.xml";String fileName = "/resource/Contract2.xml";Document document = ContractUtil.read(fileName);Element root = document.getRootElement();ContractUtil.getInterfaceParam(root, 0, "");JSONObject jsonObject = JSON.parseObject(json);matchJsonMap(jsonObject,ContractUtil.arraylist,0,"");}/*** 按xml文件解析json数据库* * @param json 需要解析的json数据* @param arrayList 获取的json数据格式的层级数据* @param i 需要循环迭代的起始值(针对list的json)* @param matchTopName 匹配上级是否一致 避免把非当前list或者map的数据解析出来*/public static void matchJsonMap(JSONObject json, ArrayList<Map<String, Object>> arrayList, int i,String matchTopName) {for (int a = i ; a < arrayList.size(); a++) {String name = (String) ContractUtil.arraylist.get(a).get("name");int level = (int) ContractUtil.arraylist.get(a).get("level");String topName = (String) ContractUtil.arraylist.get(a).get("topName");String isMap = (String) ContractUtil.arraylist.get(a).get("isMap");String isList = (String) ContractUtil.arraylist.get(a).get("isList");if ("Y".equals(isMap)) {JSONObject o = json.getJSONObject(name);if (o != null) {matchJsonMap(o, ContractUtil.arraylist, level,name);}}if ("Y".equals(isList)) {JSONArray ja = json.getJSONArray(name);if (ja != null) {for(int aa = 0; aa < ja.size(); aa++){matchJsonMap((JSONObject)ja.get(aa), ContractUtil.arraylist, aa,name);}}}if (matchTopName.equals(topName)) {if ("N".equals(isMap) && ("N".equals(isList))) {String value = json.getString(name);System.out.println("解析的字段名为:" + name + ";解析的值为:" + value + ";父层字段名称:" + topName + ";父层接口层级:"+ (0 == level ? 0 : level - 1) + ";本层层级:" + (null==topName||"".equals(topName)?level:(i+1) ));} else {System.out.println("解析的字段名为:" + name + ";解析的值为:" + " " + ";父层字段名称:" + topName + ";父层接口层级:"+ (0 == level ? 0 : level - 1) + ";本层层级:" + (i));}}}}}
在此记录一下。
0 0
- 通过解析xml获得json格式,动态解析json
- Json特殊格式解析(动态解析)
- JSON解析,XML解析
- JSON解析、XML解析
- XML解析 JSON解析
- XML解析、Json解析
- JSON解析/XML解析
- get获得json---解析
- Android中的xml、json格式数据解析
- Android解析xml和json格式数据
- iOS中解析 XML / JSON格式讲解
- Android中的xml、json格式数据解析
- Android中的xml、json格式数据解析
- Android中的xml、json格式数据解析
- 【JSON】解析JSON格式异常
- Json解析 开发实践 XML和JSON格式的数据
- .net动态解析xml和json
- JSON格式解析
- 逆向工程核心原理学习笔记(十一):栈
- 【SQL】联合语句
- Android 7.0 Gallery图库源码分析8
- Android客户端VPN及Stunnel原理理解
- Spring Boot使用Druid进行维度的统计和监控
- 通过解析xml获得json格式,动态解析json
- Java中有关null的9件事
- RGB转YUV
- python-闭包概念和append()和extend()的不同
- Charles解析https的坑
- 几种常用的下拉列表的动态加载和选中写法
- 数据结构(用C++语言描述)--第1章 绪论
- dubbo组成原理-service服务暴露
- remove-duplicates-from-sorted-list-ii