【问题标题】:How to keep the order of org.json.JSONObject?如何保持 org.json.JSONObject 的顺序?
【发布时间】:2020-04-17 23:29:58
【问题描述】:

因为要使用org.json中的转换CSV文件功能,所以必须使用org.json。 并且 csv 顺序与 JSONArray 相同,所以我需要一个正确的 Order JSONArray。 我发现 JSONArray 的顺序和放入它的顺序是一样的,但是 JSONObject 是按 'a-z' 的顺序排列的, 如何让jsonObject的顺序和我放的时候一样。 我的代码是:

org.json.JSONObject jsonObject = new org.json.JSONObject();
jsonObject.put("text1","111");
jsonObject.put("abc1","111");

org.json.JSONObject jsonObject2 = new org.json.JSONObject();
jsonObject2.put("text2","222");
jsonObject2.put("abc2","222");

JSONArray jsonArray = new JSONArray();

jsonArray.put(jsonObject);
jsonArray.put(jsonObject2);

System.out.println(jsonObject);
System.out.println(jsonObject2);
System.out.println(jsonArray);

结果是

{"abc1":"111","text1":"111"}

{"text2":"222","abc2":"222"}

[{"abc1":"111","text1":"111"},{"text2":"222","abc2":"222"}]

但我想要

{"text1":"111","abc1":"111"}

{"text2":"222","abc2":"222"}

[{"text1":"111","abc1":"111"},{"text2":"222","abc2":"222"}]

【问题讨论】:

  • JSON 对象的开头没有顺序。数组是,这就是为什么您会在数组中看到这种行为。鉴于您通过名称而不是位置访问它们的值,这并不重要。
  • 我想把json转成csv,csv顺序和JSONObect一样...
  • JSON 对象没有订单。此外,您似乎没有任何 CSV。
  • stackoverflow.com/questions/4515676/… 看到了,这就是我想做的。
  • 正如第一条评论和评分最高的答案所说...

标签: csv org.json


【解决方案1】:

最后,我使用 alibaba.json 来包含 JsonObject 的顺序。 然后我用alibaba.json lib覆盖了org.json的CDL.toString。

package com._4paradigm.repservice.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Set;

public class ConvertCsv {
    public ConvertCsv() {
    }
    public static String toString(JSONArray ja) {

        JSONObject jo = ja.getJSONObject(0);
        if (jo != null) {
            Set<String> strings = jo.keySet();
            JSONArray names = JSONArray.parseArray(JSON.toJSONString(strings));
            if (names != null) {
                return rowToString(names) + toString(names, ja);
            }
        }

        return null;
    }
    public static String rowToString(JSONArray jsonArray) {
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < jsonArray.size() ; ++i) {
            if (i > 0) {
                sb.append(',');
            }
            Object object = jsonArray.get(i);
            if (object != null) {
                String string = object.toString();
//                System.out.println(string);
                if (string.length() > 0 && (string.indexOf(44) >= 0 || string.indexOf(10) >= 0 || string.indexOf(13) >= 0 || string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
                    sb.append('"');
                    int length = string.length();

                    for(int j = 0; j < length; ++j) {
                        char c = string.charAt(j);
                        if (c >= ' ' && c != '"') {
                            sb.append(c);
                        }
                    }

                    sb.append('"');
                } else {
                    sb.append(string);
                }
            }
        }

        sb.append('\n');
        return sb.toString();
    }

    public static String toString(JSONArray names, JSONArray ja) {
        if (names != null && names.size() != 0) {
            StringBuilder sb = new StringBuilder();

            for(int i = 0; i < ja.size(); ++i) {
                JSONObject jo = ja.getJSONObject(i);
                if (jo != null) {
                    sb.append(rowToString(toJSONArray(jo, names)));
                }
            }

            return sb.toString();
        } else {
            return null;
        }
    }
    public static JSONArray toJSONArray(JSONObject obj, JSONArray names)  {
        if (names != null && !names.isEmpty()) {
            JSONArray ja = new JSONArray();

            for(int i = 0; i < names.size(); ++i) {
                ja.add(obj.get(names.getString(i)));
            }

            return ja;
        } else {
            return null;
        }
    }


    public static void main(String[] args) throws IOException {
         JSONArray array = new JSONArray();
        JSONObject object2 = new JSONObject(new LinkedHashMap<>());
        object2.put("name","LiMing");
        object2.put("age","28");
        object2.put("gender","man");

        JSONObject object = new JSONObject(new LinkedHashMap<>());
        object.put("name","LiPing");
        object.put("age","26");
        object.put("gender","women");

        array.add(object);
        array.add(object2);

        String s = ConvertCsv.toString(array);

        FileUtils.writeStringToFile(new File("E:\\hello.csv"), s);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-24
    • 2014-09-13
    • 1970-01-01
    • 2023-01-07
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    相关资源
    最近更新 更多