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

查看:450
本文介绍了在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天全站免登陆