在 JSON 转换为 CSV 期间保持 JSON 键的顺序 [英] Keep the order of the JSON keys during JSON conversion to CSV

查看:33
本文介绍了在 JSON 转换为 CSV 期间保持 JSON 键的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此处提供的 JSON 库 http://www.json.org/java/index.html 将我必须的 json 字符串转换为 CSV.但是我遇到的问题是,转换后键的顺序丢失了.

I am using the JSON library provided here http://www.json.org/java/index.html to convert a json string I have to CSV. But the problem I have is, the order of the keys is lost after conversion.

这是转换代码:

    JSONObject jo = new JSONObject(someString);
    JSONArray ja = jo.getJSONArray("items");
    String s = CDL.toString(ja);
    System.out.println(s);

这是someString"的内容:

This is the content of "someString":

{
    "items":
    [
        {
            "WR":"qwe",
            "QU":"asd",
            "QA":"end",
            "WO":"hasd",
            "NO":"qwer"
        },
    ]
}

结果如下:

WO,QU,WR,QA,NO
hasd,asd,qwe,end,qwer

虽然我期望的是保持键的顺序:

While what I expect is to keep the order of the keys:

WR,QU,QA,WO,NO
qwe,asd,end,hasd,qwer

有什么办法可以使用这个库得到这个结果吗?如果没有,是否还有其他库可以提供在结果中保持键顺序的能力?

Is there any way I can have this result using this library? If not, is there any other library that will provide the capability to keep the order of keys in the result?

推荐答案

已解决.

我使用了这里的 JSON.simple 库 https://code.google.com/p/json-simple/ 读取 JSON 字符串以保持键的顺序并从这里使用 JavaCSV 库 http://sourceforge.net/projects/javacsv/ 转换为 CSV 格式.

I used the JSON.simple library from here https://code.google.com/p/json-simple/ to read the JSON string to keep the order of keys and use JavaCSV library from here http://sourceforge.net/projects/javacsv/ to convert to CSV format.

这篇关于在 JSON 转换为 CSV 期间保持 JSON 键的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆