在Java中将嵌套的Json文件转换为CSV [英] Converting Nested Json files to CSV in java
问题描述
{
Employee:[
{
empMID:mock:1,
comments:[ ],
col1:something,
contact:[{address:2400 waterview,freetext:true}
],
性别:男性
},
{
empMID:mock:2,
注释:[],
col1: something,
contact:[{address:2200 waterview,freetext:true}
],
gender:female
}
],
cola:false,
colb:false
}
这是我的Json文件的外观。我需要将此json转换为csv。(我试图将多维数据转换为2d)。我使用gson作为我的我不能使用gson.fromgson()函数来映射模板对象,因为它应该是通用的。
我知道我们可以使用C DL将jsonarray转换为csv格式,但它不会工作在我的情况。
我的csv格式看起来像
Employee *
empMID,comment。$,contact.address,contact.freetext,gender
mock:1,,2400 waterview,TRUE,male
mock :123,,2200 waterview,TRUE,female
colA#
TRUE
colB#
FALSE
我尝试使用谷歌GSON api转换为这种格式。但我不能转换为这种格式。我用*来表示它的一个json数组和#来表示它的一个原始类型和contact.address来表示另一个json数组内部的嵌套数组。我有这个嵌套结构的问题。我能够像列一样递归地遍历所有东西。提前致谢
public static void main(String [] args)throws IOException {
BufferedReader reader = NULL;
StringBuilder content = null;
String result = null;
reader = new BufferedReader(new FileReader(temp.json));
String line = null;
content = new StringBuilder(); ((line = reader.readLine())!= null){
content.append(line);
}
reader.close();
result = content.toString();
JsonElement jelement = new JsonParser()。parse(result);
printJsonRecursive(jelement);
$ b public static void printJsonRecursive(JsonElement jelement){
if(jelement。 isJsonPrimitive()){
System.out.println(jelement.getAsString());
return;
if(jelement.isJsonArray()){
JsonArray jarray = jelement.getAsJsonArray();
for(int i = 0; i< jarray.size(); i ++){
JsonElement element = jarray.get(i);
printJsonRecursive(element);
}
return;
}
JsonObject jobject = jelement.getAsJsonObject();
设置< Entry< String,JsonElement>> set = jobject.entrySet(); (Entry< String,JsonElement> s:set)
{
printJsonRecursive(s.getValue());
}
}
}
如果你有一个映射到json的对象,你可以实现这种直接反射。
$ b
-
使用gson / jackson将json转换为java对象
-
使用反射追加字段,通过迭代类并获得任何您感兴趣的字段。
-
通过从目标对象获取值附加反射值。
更多详情请看下面的博客文章:
vcfvct.wordpress.com/2015/06/30/converting-nested-json-files-to-csv-in-java-with-reflection /
{ "Employee": [ { "empMID": "mock:1", "comments": [], "col1": "something", "contact": [{"address":"2400 waterview", "freetext":true} ], "gender": "male" }, { "empMID": "mock:2", "comments": [], "col1": "something", "contact": [{"address":"2200 waterview", "freetext":true} ], "gender": "female" } ], "cola": false, "colb": false }
This is how my Json file looks .I m required to convert this json to a csv .(I m trying to convert a multi-dimesional data to 2d).I m using gson for my purpose.I cannot use gson.fromgson() function to object map with a template because it should be generic .
I know we can use CDL to convert jsonarray to csv format but It wont work in my case .
my csv format looks like
Employee* empMID,comment.$,contact.address,contact.freetext,gender mock:1,,2400 waterview,TRUE,male mock:123,,2200 waterview,TRUE,female colA# TRUE colB# FALSE
I tried using google-GSON api to convert to this format .But I m not able to convert to this format .I have used * to represent its a json array and # to represent its a primitive type and contact.address to represent nested array inside another json array .I having problem relating this nested structure .I m able to traverse everything recursively like a column. Thanks in advance
public static void main(String[] args) throws IOException{ BufferedReader reader=null; StringBuilder content=null; String result=null; reader = new BufferedReader(new FileReader("temp.json")); String line = null; content= new StringBuilder(); while ((line = reader.readLine()) != null) { content.append(line); } reader.close(); result= content.toString(); JsonElement jelement = new JsonParser().parse(result); printJsonRecursive(jelement); } public static void printJsonRecursive(JsonElement jelement){ if(jelement.isJsonPrimitive()){ System.out.println(jelement.getAsString()); return; } if(jelement.isJsonArray()){ JsonArray jarray= jelement.getAsJsonArray(); for(int i=0;i<jarray.size();i++){ JsonElement element= jarray.get(i); printJsonRecursive(element); } return; } JsonObject jobject= jelement.getAsJsonObject(); Set<Entry<String, JsonElement>> set= jobject.entrySet(); for (Entry<String, JsonElement> s : set) { printJsonRecursive(s.getValue()); } } }
解决方案You can achieve this thru reflection if you have a object mapped to the json.
use gson/jackson to convert json to java object
append fields using reflection by iterating the class and get any field you interested in.
append value with reflection by getting value from the target object.
More detail look at my blog post below:
vcfvct.wordpress.com/2015/06/30/converting-nested-json-files-to-csv-in-java-with-reflection/
这篇关于在Java中将嵌套的Json文件转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!