简体   繁体   English

Java 中从 JSON 到 CSV 的转换器无法正常工作

[英]A converter from JSON to CSV in Java doesn't work properly

I'm doing a converter from json to csv file.我正在做一个从 json 到 csv 文件的转换器。 I've tried a solution from this link but it doesn't work properly: Converting JSON to XLS/CSV in Java我已经尝试过此链接中的解决方案,但无法正常工作: Converting JSON to XLS/CSV in Java

The problem is that I don't have data in separate columns and also I have names of columns in different order.问题是我没有单独列中的数据,而且我还有不同顺序的列名。 Is it possible to fix this?有没有可能解决这个问题?

My json looks like this:我的 json 看起来像这样:

{
 "Code":2,
 "Description":"OK",
 "Status":0,
 "Items":[{ "City":"nameOfCity",
        "Country":"nameOfCountry",
        "CountryCode":"US",
        "Latitude":"11.11111",
        "Longitude":"-11.11111",
        "Name":"name of Company",
        "Region":"nameofRegion",
        "ServisID":"111AAA111AA",
        "SiteAddress":"number and street 2301",
        "ZipCode":"1111"},
        {"City":"nameOfCity2",
        "Country":"nameOfCountry",
        "CountryCode":"US",
        "Latitude":"22.22222",
        "Longitude":"22.2222222222",
        "Name":"name of Company2",
        "Region":"nameofRegion",
        "ServisID":"111BBB111BB",
        "SiteAddress":null,
        "ZipCode":null}
        , ...etc. 

My code:我的代码:

String bodyStr = new String(proxyResponse.getBody());

JSONObject output;

try {
    output = new JSONObject(bodyStr);

    JSONArray docs = output.getJSONArray("Items");

    File file = new File("C:/folder/fromJSON.csv");
    String csv = CDL.toString(docs);
    FileUtils.writeStringToFile(file, csv);


    } catch (JSONException e) {
                e.printStackTrace();
    } catch (IOException e) {
                e.printStackTrace();
    }
}

results(first expected):结果(第一个预期):

image形象

working as you expect using the below code.使用以下代码按预期工作。 I don't see any major difference between your and my code....我看不出你的代码和我的代码有什么大的区别......

public static void main(String[] args) throws IOException {
        String inputJson = "{\"Code\":2,\"Description\":\"OK\",\"Status\":0,\"Items\":[{\"City\":\"nameOfCity\",\"Country\":\"nameOfCountry\",\"CountryCode\":\"US\",\"Latitude\":\"11.11111\",\"Longitude\":\"-11.11111\",\"Name\":\"name of Company\",\"Region\":\"nameofRegion\",\"ServisID\":\"111AAA111AA\",\"SiteAddress\":\"number and street 2301\",\"ZipCode\":\"1111\"},{\"City\":\"nameOfCity2\",\"Country\":\"nameOfCountry\",\"CountryCode\":\"US\",\"Latitude\":\"22.22222\",\"Longitude\":\"22.2222222222\",\"Name\":\"name of Company2\",\"Region\":\"nameofRegion\",\"ServisID\":\"111BBB111BB\",\"SiteAddress\":"
                + null + ",\"ZipCode\":" + null + "}]}";
        JSONObject objJsonObject = new JSONObject(inputJson);
        JSONArray objJsonArray = objJsonObject.getJSONArray("Items");
        String csv = CDL.toString(objJsonArray);
        FileUtils.writeStringToFile(new File(System.getenv("HOME")+ "/temp.csv"), csv);
}

i opened the csv file using libreoffice v5.1 with UTF-8 encoding我使用带有 UTF-8 编码的 libreoffice v5.1 打开了 csv 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM