0

Because I want to use the conversion CSV file function in org.json, I must use org.json. and the csv order is the same as JSONArray, so I need a correct Order JSONArray. I found that JSONArray order is the same as put into it, but JSONObject is order by 'a-z', How to make the order of jsonObject the same as when I put it. my code is :

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);

The result is

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

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

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

but I want

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

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

[{"text1":"111","abc1":"111"},{"text2":"222","abc2":"222"}]
rlandster
  • 7,294
  • 14
  • 58
  • 96
yeliang99
  • 63
  • 1
  • 1
  • 6

1 Answers1

0

finally, I use the alibaba.json to contains the order of JsonObject. and Then I overwrite the org.json's CDL.toString by alibaba.json lib.

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);
    }
}
yeliang99
  • 63
  • 1
  • 1
  • 6