这是我的 Json 响应:
{
"value": [
{
"type": "school",
"id": 12,
"name": "NNPS",
"city": "CA",
"geo_position": {
"latitude": 52.52437,
"longitude": 13.41053
}
},
{
"type": "College",
"id": 44,
"name": "PEC",
"city": "PE",
"geo_position": {
"latitude": 45.50298,
"longitude": 10.04366
}
}
]
}
这是我使用“json.JSONArray”库的java代码:
while ((csvdata= br.readLine()) != null) {
JSONObject output= new JSONObject(csvdata);
JSONArray docs = output.getJSONArray("value");
String csv = CDL.toString(docs);
FileUtils.writeStringToFile(file, csv);
}
当我检查 csv
文件时,它显示如下:
但应该是这样的:
我正处于编码阶段的最初阶段,目前正在对其进行改进。请给我一些建议:) 我应该如何更改它?
最佳答案
您应该重新设置纬度和经度的级别。这对我有用
while ((csvdata= br.readLine()) != null) {
JSONObject output= new JSONObject(csvdata);
JSONArray docs = output.getJSONArray("value");
for(int i=0; i<docs.length();i++){
JSONObject geo_pos = (JSONObject)(docs.getJSONObject(i).getJSONObject("geo_position"));
docs.getJSONObject(i).put("latitude", geo_pos.get("latitude"));
docs.getJSONObject(i).put("longitude", geo_pos.get("longitude"));
docs.getJSONObject(i).remove("geo_position");
}
String csv = CDL.toString(docs);
FileUtils.writeStringToFile(file, csv);
}
关于java - 如何使用 json.JSONArray 将多维 Json 数组解析为 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20446862/