【问题标题】:JSON to CSV file conversionJSON 到 CSV 文件的转换
【发布时间】:2014-05-30 06:32:15
【问题描述】:

我有一个 JSON 文件

{
  "name": "re2",
  "count": 1806,
  "frequency": "realtime",
  "version": 5,
  "newdata": true,
  "lastrunstatus": "success",
  "lastsuccess": "Fri May 30 2014 06:02:41 GMT+0000 (UTC)",
  "results": {
    "collection1": [
      {
        "Title": {
          "href": "http://www.realestate.com.au/project-spec+property+%e2%80%93+helio+apartments-vic-north+melbourne-600004887",
          "text": "93 Flemington Road, North Melbourne, Vic 3051"
        },
        "image": {
          "href": "http://www.realestate.com.au/project-spec+property+%e2%80%93+helio+apartments-vic-north+melbourne-600004887",
          "alt": "93 Flemington Road, North Melbourne, Vic 3051",
          "src": "http://i2.au.reastatic.net/345x200/3a7e58fe3aefa7fd373c1b9c9879d648257dc0e7c8d35c6b7a19d261bffeff28/main.jpg"
        },
        "price": "$700,000 - $770,000",
        "title2": {
          "href": "http://www.realestate.com.au/project-spec+property+%e2%80%93+helio+apartments-vic-north+melbourne-600004887",
          "text": "93 Flemington Road, North Melbourne, Vic 3051"
        }
      },
      {
        "Title": {
          "href": "http://www.realestate.com.au/project-redmond+park-vic-carlton+north-600002807",
          "text": "300 Pigdon Street, Carlton North, Vic 3054"
        },
        "image": {
          "href": "http://www.realestate.com.au/project-redmond+park-vic-carlton+north-600002807",
          "alt": "300 Pigdon Street, Carlton North, Vic 3054",
          "src": "http://i2.au.reastatic.net/345x200/c22712b2c40db6e8017ebc6f677c9835991e3e1ab9431cfe28d0ab8ea0af43e3/main.jpg"
        },
        "price": "$830,000 - $880,000",
        "title2": {
          "href": "http://www.realestate.com.au/project-redmond+park-vic-carlton+north-600002807",
          "text": "300 Pigdon Street, Carlton North, Vic 3054"
        }
      },
      {
        "Title": {
          "href": "http://www.realestate.com.au/property-house-vic-kensington-116973739",
          "text": "60 WOLSELEY PARADE, Kensington, Vic 3031"
        },
        "image": {
          "href": "http://www.realestate.com.au/property-house-vic-kensington-116973739",
          "alt": "60 WOLSELEY PARADE, Kensington, Vic 3031",
          "src": "http://i4.au.reastatic.net/355x265/e026805577c810c6df722c171a00786f4510cb959390375d589e2d1d90ef2461/main.jpg"
        },
        "price": "SOLD $1,360,000",
        "title2": {
          "href": "http://www.realestate.com.au/property-house-vic-kensington-116973739",
          "text": "60 WOLSELEY PARADE, Kensington, Vic 3031"
        }
      },

我尝试了许多在线 JSON 到 CSV 转换器,该文件无法正确转换。

我想要一个 CSV 文件 标题、href、文本、图片、href、alt、src、价格

由于文件的复杂性,我没有任何在线教程。

【问题讨论】:

    标签: json csv


    【解决方案1】:

    一般来说,自动将 JSON 转换为 CSV 是不可能的,因为一个是对象图,另一个本质上是一个表格。这就像试图将 Cube 转换为圆形一样。

    更复杂的是,您的 JSON 似乎不是同质的。最明显的是第一个条目没有 title2 实例。为了解决这个问题,我将其分解为以下步骤:

    1. 将 JSON 转换为正确的 ObjectGraph。
    2. 将对象图折叠成一个平面列表。根据需要映射字段。
    3. 将列表写入 CSV。

    从您的示例 JSON 中无法立即看出将其全部读入内存是否可行,或者您是否需要将其分解,一次转换一项。我怀疑你会找到一个可以为你做这件事的在线工具,因为你的 JSON 看起来不连贯,而且你的需求非常具体。

    【讨论】:

    • 谢谢,我会试试你的建议
    • 很酷,让我知道你的进展如何。
    【解决方案2】:

    我刚刚发布了一个在 Node.js 中简化此过程的模块

    var jsonexport = require('jsonexport');
    
    var contacts = [{
       name: 'Bob',
       lastname: 'Smith',
       family: {
           name: 'Peter',
           type: 'Father'
       }
    },{
       name: 'James',
       lastname: 'David',
       family:{
           name: 'Julie',
           type: 'Mother'
       }
    },{
       name: 'Robert',
       lastname: 'Miller',
       family: null,
       location: [1231,3214,4214]
    },{
       name: 'David',
       lastname: 'Martin',
       nickname: 'dmartin'
    }];
    
    jsonexport(contacts,function(err, csv){
        if(err) return console.log(err);
        console.log(csv);
    });
    

    https://www.npmjs.com/package/jsonexport

    【讨论】:

      【解决方案3】:

      有一个名为json2flat的库。
      它的作用是获取一个复杂的 JSON 文档并将其转换为 CSV 格式。

      所以你需要做的就是将你的 java 对象转换成 JSON 格式。之后 您需要将生成的 JSON 传递到库中,它会返回 2D JSON 的表示形式,您也可以从中获取 csv。

      这个库不是那么成熟,但它仍然很有前途。 你应该试一试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-03
        • 1970-01-01
        相关资源
        最近更新 更多