Java学习之String转换成JSONObject/JSONArray时保证顺序不变

com.alibaba.fastjson.JSONObject:

String在转JSONObject,顺序可能会改变,这样如果需要对结果进行循环时就无法保证顺序和想要的结果一致。

例如:

        String textObject = "{"row1":{"userNmae":"zhangsan"},"row2":{"userNmae":"lisi"},"row3":{"userNmae":"wangwu"}}";
        JSONObject json1 = JSONObject.parseObject(textObject);
        Iterator<String> iterator = json1.keySet().iterator();
        int i = 1;
        while (iterator.hasNext()) {
            String key = iterator.next();
            System.out.println(i + " : " + json1.get(key));
            i++;
        }

想要结果:

1 : {"userNmae":"zhangsan"}
2 : {"userNmae":"lisi"}
3 : {"userNmae":"wangwu"}

实际结果:

1 : {"userNmae":"zhangsan"}
2 : {"userNmae":"wangwu"}
3 : {"userNmae":"lisi"}

 解决办法:

转换时加上:Feature.OrderedField

例如:

        String textObject = "{"row1":{"userNmae":"zhangsan"},"row2":{"userNmae":"lisi"},"row3":{"userNmae":"wangwu"}}";
        int i = 1;
        JSONObject json2 = JSONObject.parseObject(textObject, Feature.OrderedField);
        Iterator<String> iterator2 = json2.keySet().iterator();
        while (iterator2.hasNext()) {
            String key = iterator2.next();
            System.out.println(i + " : " + json2.get(key));
            i++;
        }

想要结果:

1 : {"userNmae":"zhangsan"}
2 : {"userNmae":"lisi"}
3 : {"userNmae":"wangwu"}

实际结果:

1 : {"userNmae":"zhangsan"}
2 : {"userNmae":"lisi"}
3 : {"userNmae":"wangwu"}

com.alibaba.fastjson.JSONArray:

 根据JSONObject拓展一下:假如JSONArray中包含JSONObject,在调用“JSONObject.parseArray()”时也无法保证顺序。

例如:

       String textArray = "[{"people":{"row1":{"userNmae":"zhangsan"},"row2":{"userNmae":"lisi"},"row3":{"userNmae":"wangwu"}}}]";
        JSONArray array1 = JSONObject.parseArray(textArray);
        for (int i = 0; i < array1.size(); i++) {
            JSONObject jsonObject = array1.getJSONObject(i);
            Iterator<String> iterator = jsonObject.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                JSONObject inner = jsonObject.getJSONObject(key);
                Iterator<String> innerIter = inner.keySet().iterator();
                int flag = 1;
                while (innerIter.hasNext()) {
                    String innerKey = innerIter.next();
                    String innerValue = inner.getString(innerKey);
                    System.out.println(flag + " : " + innerValue);
                    flag++;
                }
            }
        }

想要结果:

1 : {"userNmae":"zhangsan"}
2 : {"userNmae":"lisi"}
3 : {"userNmae":"wangwu"}

实际结果:

1 : {"userNmae":"zhangsan"}
2 : {"userNmae":"wangwu"}
3 : {"userNmae":"lisi"}

解决办法:

参考JSONObject,加 “Feature.OrderedField”,并且转换一下JSONArray的解析方式。

    String textArray = "[{"people":{"row1":{"userNmae":"zhangsan"},"row2":{"userNmae":"lisi"},"row3":{"userNmae":"wangwu"}}}]";
        //不直接转Array,调用parse()方法,当作JSONObject转,然后强制转换。
        JSONArray array1 = (JSONArray) JSONObject.parse(textArray, Feature.OrderedField);
        for (int i = 0; i < array1.size(); i++) {
            JSONObject jsonObject = array1.getJSONObject(i);
            Iterator<String> iterator = jsonObject.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                JSONObject inner = jsonObject.getJSONObject(key);
                Iterator<String> innerIter = inner.keySet().iterator();
                int flag = 1;
                while (innerIter.hasNext()) {
                    String innerKey = innerIter.next();
                    String innerValue = inner.getString(innerKey);
                    System.out.println(flag + " : " + innerValue);
                    flag++;
                }
            }
        }

实际结果:

1 : {"userNmae":"zhangsan"}
2 : {"userNmae":"lisi"}
3 : {"userNmae":"wangwu"}

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注