递归案列(将JSON某个节点数据转成json数组)

将配置的xml指定节点下的数据对应json的子节点数据转成json数组
/**
	 * 	将配置的xml指定节点下的数据对应json的子节点数据转成json数组
	 * @param intfConfig 
	 * @param respdata  XML转成json的字符串
	 * @return json对象
	 */
	public static Object constructJSON(String[] singleXmlNodes, JSONObject parentJsonObject,int index) {
		
		boolean isEnd = false;
		if(singleXmlNodes.length == (index+1)) {
			isEnd = true;
		}
		
		if(JSONTools.isJSONObject(parentJsonObject, singleXmlNodes[index])) {
			JSONObject jsonObject =  parentJsonObject.getJSONObject(singleXmlNodes[index]);
			if(jsonObject==null) {
				throw new RuntimeException("xml指定节点下无数据");
			}
			if (!isEnd) {
				 parentJsonObject.put(singleXmlNodes[index],constructJSON(singleXmlNodes,jsonObject,++index));
			}else {
				JSONArray jsonArray = new JSONArray();
				jsonArray.add(jsonObject);
				parentJsonObject.put(singleXmlNodes[index],jsonArray);
			}
		}else {
			JSONArray jsonArray = parentJsonObject.getJSONArray(singleXmlNodes[index]);
			if(jsonArray == null ) {
				throw new RuntimeException("xml指定节点下无数据");
			}
			if(!isEnd) {
				index++;
				for(int i=0; i<jsonArray.size(); i++) {
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					jsonArray.remove(i);
					jsonArray.add(i,constructJSON(singleXmlNodes, jsonObject, index));
				}
				parentJsonObject.put(singleXmlNodes[--index], jsonArray);
			}
		}
		return parentJsonObject;
	}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值