解析jsonobject

package com;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ParseJson {
	private static String json="{\"111400029\":{\"desc\":\"实例的当前状态或类型不支持清空操作。\"},\"111400135\":{\"desc\":[{\"min\":\"前实例已被锁定,请{1}分钟后重试。\",\"mins\":\"当前实例已被锁定,请{1}分钟后重试。\",\"sed\":\"当前实例已被锁定,请{1}秒后重试。\",\"seds\":\"当前实例已被锁定,请{1}秒后重试。\"}]},\"111400142\":{\"desc\":\"实例正在清空数据中,请稍后重试。\"},\"111409064\":{\"desc\":\"已有订单未处理完成。\"},\"Common.0000\":{\"desc\":\"系统繁忙,请稍后再试。\"},\"DCS.5001\":{\"desc\":\"操作失败。该功能需要进行升级之后才能使用,请联系管理员升级。\"}}";
	public static void main(String[] args) {
		System.out.println(parse(JSON.parseObject(json)));
	}
	
	public static Map<String,String> parse(JSONObject jsonObject){
		Map<String,String> result = new LinkedHashMap<String,String>(); 
		if(jsonObject == null) {
			return result;
		}
		toParse(jsonObject,"",result);
		return result;
	}
	
	private static void toParse(JSONObject jsonObject,String codeId,Map<String,String> result) {
		for(Map.Entry<String,Object> entry:jsonObject.entrySet()) {
			Object val = entry.getValue();
			if(val instanceof String) {
				result.put((StringUtils.isEmpty(codeId) ? "" : codeId + "-") + entry.getKey(), String.valueOf(val));
			}else if(val instanceof JSONObject){
				toParse((JSONObject)val,(StringUtils.isEmpty(codeId) ? "" : codeId + "-") + entry.getKey(),result);
			}else if(val instanceof JSONArray) {
				for(Object temp:(JSONArray)val) {
					if(temp instanceof JSONObject) {
						toParse((JSONObject)temp,(StringUtils.isEmpty(codeId) ? "" : codeId + "-") + entry.getKey(),result);
					}
				}
			}
		}
		
	}
	
	
}

猜你喜欢

转载自blog.csdn.net/weixin_41796956/article/details/85321341
今日推荐