JSON到CSV文件转换

JSON到CSV文件转换,json,csv,Json,Csv,我有一个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": [ {

我有一个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,price


由于文件的复杂性,我不太可能在线学习任何教程。

通常不可能自动将JSON转换为CSV,因为一个是对象图,另一个本质上是表。这就像试图将一个立方体转换成一个圆

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

  • 将JSON转换为适当的ObjectGraph
  • 将对象图折叠到平面列表中。根据需要映射字段
  • 将列表写入CSV

  • 从您的示例JSON看,是否可以一次将其全部读入内存,或者是否需要将其分解,一次转换一个项目,这一点并不明显。我怀疑你是否能找到一个在线工具来帮你做到这一点,因为你的JSON看起来不连贯,你的需求也非常具体。

    我刚刚在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);
    });
    

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

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

    这个库还不太成熟,但仍然很有前途。
    你应该试一试。

    谢谢,我会试试你的建议。很好,让我知道你是怎么做的。