【问题标题】:How to parse MultiDimension Json Array into CSV using json.JSONArray如何使用 json.JSONArray 将多维 Json 数组解析为 CSV
【发布时间】:2013-12-07 21:26:27
【问题描述】:

这是我的 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 文件时,它显示如下:

不过应该​​是这样的:

我正处于编码阶段的初期,目前正在改进它。请给我一些建议:) 应该怎么改?

【问题讨论】:

  • 为什么会这样?你做了什么来促成这种转变?

标签: java json csv export-to-csv jsonobject


【解决方案1】:

您应该再次设置 lat 和 long 的水平。这对我有用

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);
 }

【讨论】:

  • 我试过这种方法。对于具有空值的键,我无法在最终响应中获得一列。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-02
  • 2014-01-06
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
相关资源
最近更新 更多