[英]how to convert a CSV into JSON using Python Or Java. The csv is representation of a nested JSON (Containing array of json objects and nested objects)
[英]How to convert sequential JSON objects into csv using java
我有一个 JSON 文件,其数据格式如下
{"sum_message_count":"66","api_product":"Test1","response_status_code":"200"}
{"sum_message_count":"8","api_product":"Test2","response_status_code":"201"}
{"sum_message_count":"2","api_product":"Test3","response_status_code":"201"}
{"sum_message_count":"62","api_product":"Test4","response_status_code":"201"}
So I want these JSON objects into csv as following
sum_message_count api_product response_status_code 66 Test1 200 8 Test2 201 2 Test3 201 22 Test4 201
有人可以帮助解决java(主程序)中的代码片段吗
String input = "[" +
"{\"sum_message_count\":\"66\",\"api_product\":\"Test1\",\"response_status_code\":\"200\"},\n" +
"{\"sum_message_count\":\"8\",\"api_product\":\"Test2\",\"response_status_code\":\"201\"},\n" +
"{\"sum_message_count\":\"2\",\"api_product\":\"Test3\",\"response_status_code\":\"201\"},\n" +
"{\"sum_message_count\":\"62\",\"api_product\":\"Test4\",\"response_status_code\":\"201\"}" +
"]";
List<String> headers = Arrays.asList("sum_message_count", "api_product", "response_status_code");
ObjectMapper mapper = new ObjectMapper();
List<Object> messages = mapper.readValue(input, List.class);
StringBuilder data = new StringBuilder();
for (int i = 0; i < messages.size(); i++) {
LinkedHashMap<String, String> msg = (LinkedHashMap<String, String>) messages.get(i);
data.append(String.format("%s %s %s ",
msg.get(headers.get(0)),
msg.get(headers.get(1)),
msg.get(headers.get(2))));
}
System.out.println(headers);
System.out.println(data.toString());
//write file here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.