【问题标题】:A converter from JSON to CSV in Java doesn't work properlyJava 中从 JSON 到 CSV 的转换器无法正常工作
【发布时间】:2016-11-22 02:46:57
【问题描述】:

我正在做一个从 json 到 csv 文件的转换器。我已经尝试了这个链接的解决方案,但它不能正常工作:Converting JSON to XLS/CSV in Java

问题是我没有单独的列中的数据,而且我有不同顺序的列名。有可能解决这个问题吗?

我的 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. 

我的代码:

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

结果(第一个预期):

image

【问题讨论】:

    标签: java json csv


    【解决方案1】:

    使用以下代码按预期工作。我看不出你的代码和我的代码有什么大的区别......

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

    我使用带有 UTF-8 编码的 libreoffice v5.1 打开了 csv 文件

    【讨论】:

    • 我试过 libreoffice v5.1 而不是 Microsoft Excel,它可以工作,但它以不同的顺序设置列的名称。它不是从“城市”开始的。
    • 而且当我用记事本打开我的文件时,它的格式与您输入的不同:“SiteAddress,ServisID,ZipCode,Country,Region,Latitude,City,CountryCode,Longitude,Name”,也许就是这样问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多