xml转json,实现未知json中寻找指定字段的值

  • 需求:接收xml或者json,从中获取指定字段的值,该值可能为value、jsonArray数组、jsonObject。
  • 分析:同一采用对json进行查找的方式,如果接收的是xml则将xml转换为json进行操作。

xml转json

JSONObject xmlJSONObj = XML.toJSONObject(xmlString);

其中用到json-java

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20160810</version>
</dependency>

获取指定字段的值

/**
     * 检查json类型
     * @param json json对象
     * @return  1:jsonObject  2:jsonArray  3:值value
     */
    public static int checkType(String json){  
        try {  
            JSONObject object = new JSONObject(json);  
            return 1;  
        } catch (JSONException e) {
            try {  
                JSONArray array = new JSONArray(json);  
                return 2;  
            } catch (JSONException e1) {  
                return 3;  
            }  
        }  
    }  

/**
     * 获取目标值
     * @param xmlJSONObj json对象
     * @param goalKey 要查找的值
     * @return
     */
    private Object getGoalValue(JSONObject xmlJSONObj, String goalKey) {
        JSONObject json = xmlJSONObj;
        Object goalValue = null;
        if (json.has(goalKey)) {
            return json.get(goalKey);
        } else {
        Iterator ite = json.keys();
        int i;
        List<String> list = new ArrayList<String>();
        while (ite.hasNext()) {
            String k = (String) ite.next();

            i = checkType(json.get(k).toString());
            if (1 == i) {
                JSONObject jsonObj = json.getJSONObject(k);
                goalValue = getGoalValue(jsonObj,goalKey);
            } else if (2 == i) {
                JSONArray jsonArr = json.getJSONArray(k);
                Iterator itArr = jsonArr.iterator();
                while (itArr.hasNext()) {
                    JSONObject job = (JSONObject) itArr.next();
                    goalValue = getGoalValue(job,goalKey);
                    //①考虑目标结果要取一个json数组下不同对象的值
                    if (null != goalValue) {
                        list.add(goalValue.toString());
                    }
                    //②不考虑目标结果要多个值的情况(用①代码则注释②代码,反之)
                    /*if (null != goalValue) {
                        break;
                    }*/
                }
                //①考虑目标结果要取一个json数组下不同对象的值
                goalValue = list.toString().substring(1, list.toString().length()-1);
            }
        } 
        }
        return goalValue;
    }

public static void main(String[] args) {
        Xml2JsonUtil xml = new Xml2JsonUtil();
        //获取xml
        try {
            Document doc = new SAXReader().read(new File("e:/demo.xml"));
            String str = doc.asXML();
            JSONObject xmlJSONObj = XML.toJSONObject(str);
            //设置缩进
            String json = xmlJSONObj.toString(4);
            //输出格式化后的json
            System.out.println(json);
            //目标值key
            String goalKey = "code";
            Object goalValue = xml.getGoalValue(xmlJSONObj,goalKey);
            System.out.println("目标值是:" + goalValue);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

猜你喜欢

转载自blog.csdn.net/kiana168/article/details/77680012