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

2021/9/18 17:07:23

本文主要是介绍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"}

 



这篇关于Java学习之String转换成JSONObject/JSONArray时保证顺序不变的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程