![](/img/trans.png)
[英]CSV to JSON with Jackson in java, how to make nested objects?
[英]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)
我得到一個 csv 文件,例如:
attrib1_x, attrib1_y, attrib1_z_0_p, attrib1_z_0_c, attrib1_z_1_p, attrib1_z_1_c, attrib2_R, attrib2_K, attrib3
1, 2, 100, 200, 500, 600, 222, 320,你好
csv 代表如下所示的 json。
{
"attrib1":{
"x":1,
"y":2,
"z":[{"p":100,"c":200},{"p":500,"c":600}]
},
"attrib2":{"R":222,"K":320},
"attrib3":"hello"
}
. 不知道該怎么做。 是否有任何庫(Python/Java)可以幫助我解決這個問題。
如果有不同的 csv header 可用的任何解決方案/建議也將有效。 我可以要求團隊為我提供 csv 和不同的 header 名稱來表示嵌套的 / arrays。
JSONObject jsonObject = new JSONObject("{
"attrib1":{
"x":1,
"y":2,
"z":[{"p":100,"c":200},{"p":500,"c":600}]
},
"attrib2":{"R":222,"K":320},
"attrib3":"hello"
}");
試一試。 它可能會有所幫助
首先你迭代key
然后類似地迭代value
並保存到 csv
String eol = System.getProperty("line.separator");
try(Writer writer = new FileWriter("C:\\Users\\m.hussain\\Desktop\\CSV\\testing.csv"))
{
for (Map<String, Object> map : objectRecords)
{
for (Map.Entry<String, Object> entry : map.entrySet())
{
writer.append(entry.getKey())
.append(',');
}
break;
}
writer.append(eol);
for (Map<String, Object> map : objectRecords)
{
for (Map.Entry<String, Object> entry : map.entrySet())
{
JSONObject jsonObject;
try
{
jsonObject = new JSONObject(String.valueOf(entry.getValue()));
writer.append(jsonObject.getString("value"));
}
catch (Exception e)
{
writer.append(String.valueOf(entry.getValue()));
}
writer.append(',');
}
writer.append(eol);
}
你的CSV第一行是字段名(用下划線表示層級關系),從第二行開始是明細數據,需要把CSV轉成JSON格式。 這里的難點是動態解析。 會涉及到分組、遞歸、循環、條件判斷和字符串拼接。 如果您嘗試在 Java 中執行此操作,代碼將相當冗長。
嘗試使用 SPL,即開源 Java package 來執行此操作。 這很簡單,五行代碼就足夠了:在此處輸入圖像描述
SPL 提供 JDBC 驅動程序,供 Java 調用。只需將上述 SPL 腳本存儲為 recurse.splx,然后在調用存儲過程時在 Java 應用程序中調用它:
…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st = con.prepareCall("call recurse()");
st.execute();
…
使用庫Josson構建 JSON 結構。
首先將csv文件header和內容進行拆分,構建一個Map。然后使用function unflatten()
進行轉換。
LinkedHashMap<String, Object> csv = new LinkedHashMap<>();
csv.put("attrib1_x", 1);
csv.put("attrib1_y", 2);
csv.put("attrib1_z_0_p", 100);
csv.put("attrib1_z_0_c", 200);
csv.put("attrib1_z_1_p", 500);
csv.put("attrib1_z_1_c", 600);
csv.put("attrib2_R", 222);
csv.put("attrib2_K", 320);
csv.put("attrib3", "hello");
Josson josson = Josson.from(csv);
System.out.println(josson.getNode().toPrettyString()); // before unflatten
JsonNode node = josson.getNode("unflatten('_')");
System.out.println(node.toPrettyString()); // after unflatten
Output:展開前
{
"attrib1_x" : 1,
"attrib1_y" : 2,
"attrib1_z_0_p" : 100,
"attrib1_z_0_c" : 200,
"attrib1_z_1_p" : 500,
"attrib1_z_1_c" : 600,
"attrib2_R" : 222,
"attrib2_K" : 320,
"attrib3" : "hello"
}
Output:展開后
{
"attrib1" : {
"x" : 1,
"y" : 2,
"z" : [ {
"p" : 100,
"c" : 200
}, {
"p" : 500,
"c" : 600
} ]
},
"attrib2" : {
"R" : 222,
"K" : 320
},
"attrib3" : "hello"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.