本文介绍了扁平化JSON来csv格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图一个JSON值转换为基于字段平面CSV由用户选择。我的JSON看起来像

i am trying to convert a json value to a flat csv based on the field that is selected by user . My json looks like

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,但我想从根本上拯救自己的细节。

Ive been able to collect the values of all occurences and store it as a csv, but I want to save the details from the root itself..

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

If I select Time, the csv output should look like,

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

是否有可能扁平化JSON ..我将添加JSON小提琴链接显示在哪里伊夫这个东西..达成

推荐答案

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

Your data value is not a JSON (string) - it's an object. There are many ways to 'flatten' this object, may be this little function might be helpful:

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

和的它如何被使用。 )

And here's how it can be used. )

这篇关于扁平化JSON来csv格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:53