2012-07-04 28 views
1

我想根据用户选择的字段将json值转换为平面csv。我的JSON看起来像flattening json格式为csv

var data = { 
"_index": "test", 
"_type": "news", 
"_source": { 
    "partnerName": "propertyFile 9", 
    "relatedSources": "null", 
    "entityCount": "50", 
    "Categories": { 
     "Types": { 
      "Events": [{ 
       "count": 1, 
       "term": "Time", 
       "Time": [{ 
        "term": "Dec 9", 
        "Dec_9": [{ 
         "count": 1, 
         "term": "2012" 
        }] 
        }] 
       }, { 
       "count": 4, 
       "term": "News", 
       "News": [{ 
        "term": "Germany", 
        "Germany": [{ 
         "count": 1, 
         "term": "Election" 
        }], 
        "currency": "Euro (EUR)" 
       }, { 
        "term": "Egypt", 
        "Egypt": [{ 
         "count": 1, 
         "term": "Revolution" 
        }] 
        }] 
       }] 
      } 
    } 
}}; 

的Ive能够收集到所有出现的值,并将其存储为CSV,但我想从根本上本身保存的细节..

如果我选择的时间, CSV输出应该是什么样子,

"test", "news", "propertyFile 9","null", "50", "Events": "Time", "Dec 9", "2012" 

是否有可能实现平坦化JSON ..我会添加JSON小提琴链接显示在那里伊夫这个东西达到.. http://jsfiddle.net/JHCwM/

+0

JSON只是以字符串形式一个JavaScript数据结构。你不直接处理json - 你处理原生javascript数据并从那里开始工作。 –

回答

2

data值不是一个JSON(字符串) - 它是一个对象。有许多方法来“扁平化”这一目的,可能是这个小功能可能会有所帮助:

var recMap = function(obj) { 
    return $.map(obj, function(val) { 
    return typeof val !== 'object' ? val : recMap(val); 
    }); 
} 

而且here的它如何被使用。 )

+0

jsfiddle的例子是ntwrking ..是那个jQuery的? – user1371896

+0

请定义'不工作'。你的意思是你在'console'中看不到任何东西? – raina77ow

+0

是...控制台是nt打印任何输出 – user1371896

0

检查了这一点扁平化JSON的

// Convert Nested Json to Flat Json 
 
// Check the final json in firebug console. 
 
var fullData = {"data":[{"Vehicle":"BMW","Date":"30, Jul 2013 09:24 AM","Location":"Hauz Khas, Enclave, New Delhi, Delhi, India","Speed":42,"Children":[{"Vehicle":"BMW","Date":"30, Jul 2013 09:24 AM","Location":"Hauz Khas, Enclave, New Delhi, Delhi, India","Speed":42,"Children":[{"Vehicle":"BMW","Date":"30, Jul 2013 09:24 AM","Location":"Hauz Khas, Enclave, New Delhi, Delhi, India","Speed":42,"Children":[]}]},{"Vehicle":"Honda CBR","Date":"30, Jul 2013 12:00 AM","Location":"Military Road, West Bengal 734013, India","Speed":0,"Children":[]}]},{"Vehicle":"Honda CBR","Date":"30, Jul 2013 12:00 AM","Location":"Military Road, West Bengal 734013, India","Speed":0,"Children":[]},{"Vehicle":"Supra","Date":"30, Jul 2013 07:53 AM","Location":"Sec-45, St. Angel's School, Gurgaon, Haryana, India","Speed":58,"Children":[]},{"Vehicle":"Land Cruiser","Date":"30, Jul 2013 09:35 AM","Location":"DLF Phase I, Marble Market, Gurgaon, Haryana, India","Speed":83,"Children":[]},{"Vehicle":"Suzuki Swift","Date":"30, Jul 2013 12:02 AM","Location":"Behind Central Bank RO, Ram Krishna Rd by-lane, Siliguri, West Bengal, India","Speed":0,"Children":[]},{"Vehicle":"Honda Civic","Date":"30, Jul 2013 12:00 AM","Location":"Behind Central Bank RO, Ram Krishna Rd by-lane, Siliguri, West Bengal, India","Speed":0,"Children":[]},{"Vehicle":"Honda Accord","Date":"30, Jul 2013 11:05 AM","Location":"DLF Phase IV, Super Mart 1, Gurgaon, Haryana, India","Speed":71,"Children":[]}]} 
 
var finalData = []; 
 
loopJson(fullData.data); 
 
function loopJson(data) { 
 
    $.each(data, function(i, e) { 
 
     if (e.Children.length>0) { 
 
      var ccd = e.Children; 
 
      delete e.Children; 
 
      finalData.push(e); 
 
      loopJson(ccd); 
 
     } else { 
 
      delete e.Children; 
 
      finalData.push(e); 
 
     } 
 
    }); 
 
} 
 
console.log(finalData);

这里是Js小提琴链接http://jsfiddle.net/2nwm43yc/

0

这是另一种方法扁平化对象到键/值对,其中键是属性的完整路径。

let data = { 
 
    pc: "Future Crew", 
 
    retro: { 
 
    c64: "Censor Design", 
 
    amiga: "Kefrens" 
 
    } 
 
}; 
 

 
let flatten = (obj, path = []) => { 
 
    return Object.keys(obj).reduce((result, prop) => { 
 
    if (typeof obj[prop] !== "object") { 
 
     result[path.concat(prop).join(".")] = obj[prop]; 
 
     return result; 
 
    } 
 
    return Object.assign(result, flatten(obj[prop], path.concat(prop), result)); 
 
    }, {}); 
 
} 
 

 
console.log(
 
    flatten(data) 
 
);