2012-07-04 66 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) 
 
);