反转JSON数组

时间:2014-04-18 09:37:20

标签: java json csv

String jsonString="{"name":"event_level_count","elements":[{"serial_number":"xxx12315","manufacturer_name":"xxx","count_level1":2004,"count_level2":1798,"count_level3":7},{"serial_number":"yyx01444","manufacturer_name":"xxx","count_level1":15,"count_level2":11,"count_level3":3}]}" ;
    JSONObject output = new JSONObject(jsonString);
    JSONArray docs = output.getJSONArray("elements");
    System.out.println("Docs: "+docs);

输出文档:

    {"name":"event_level_count","elements":[{"count_level3":7,"count_level2":1798,"count_level1":2004,"manufacturer_name":"xxx","serial_number":"xxx12315"},{"count_level3":3,"count_level2":11,"count_level1":15,"manufacturer_name":"xxx","serial_number":"yyx01444"}]}

    File file=new File("path \\ExportAsExcelfromJSON.csv");
    String csv = CDL.toString(docs);

我需要将其反转为给定的字符串--jsonString。

1 个答案:

答案 0 :(得分:4)

JSON只是一种保存或传输数据的方式 - 如果你关心它的外观(例如它是如何排序的),你可能做错了什么。如果您希望以任何方式对数组进行排序,请在将JSON解析回实际数据后再执行此操作。大多数编程语言(例如Java)都带有易于使用的工具来对数组进行排序。

根据Nizil的评论和this page,您可以使用Java中reverse(List<?> list)类的Collections函数。