将javascript对象转换为有序的逗号分隔值

时间:2012-07-12 12:41:58

标签: javascript arrays json recursion hash

我试图让json中的项目有条不紊地排列。我能够选择json中存在的“term”值,但是可以按照我在预期输出部分中显示的方式进行排列吗?我添加了一个jsfiddle链接来显示我到达的位置:

[{
        "_index": "test",
        "id": "YMFT112",
        "_source": {
           "Author": "SAM",

                    "Map": [
                        {
                            "count": 1,
                            "term": "Company",
                            "Company": [
                                {
                                    "sector": "Technology",
                                    "ticker": "AAPL",
                                    "Apple_Inc": [
                                        {
                                            "count": 1,
                                            "term": "Apple"
                                        }
                                    ],
                                    "term": "Apple Inc",
                                    "type": "BCap"
                                }
                            ]
                        },
                        {
                            "count": 1,
                            "term": "Country",
                            "Country": [
                                {
                                    "region": "North Americas",
                                    "index": "DOW JONES INDUS. AVG , S&P 500 INDEX , NASDAQ COMPOSITE INDEX",
                                    "United_States": [
                                        {
                                            "count": 1,
                                            "term": "United States"
                                        }
                                    ],
                                    "term": "United States",
                                    "currency": "Dollar (USD)",
                                    "Canada": [
                                        {
                                            "count": 1,
                                            "term": "Canada",

                                            "Canada" : [{
                                                "count": 1,
                                                "term":"Toronto"
                                            }]


                                        }
                                    ],
                                    "term": "Canada",
                                    "currency": "Dollar (USD)"


                                }
                            ]
                        },

                        {
                            "count": 5,
                            "term": "Personality",
                            "Personality": [
                                {
                                    "count": 1,
                                    "term": "Bart Prince"
                                },
                                {
                                    "count": 1,
                                    "term": "Thomas"
                                },
                                {
                                    "count": 1,
                                    "term": "Deborah Hornstra"
                                },
                                {
                                    "count": 1,
                                    "term": "Henderson Sotheby"
                                },
                                {
                                    "count": 1,
                                    "term": "Max Alliance"
                                }

                            ]
                        }
                    ]
                }
            ,
            "Link": "http://testLink.com/1"
         },
        {
        "_index": "test",
        "_id": "YMFT113",
        "_source": {
           "Author": "MAX",

                    "Map": [
                        {
                            "count": 1,
                            "term": "Company",
                            "Company": [
                                {
                                    "sector": "Technology",
                                    "ticker": "AAPL",
                                    "Microsoft Corp": [
                                        {
                                            "count": 1,
                                            "term": "Microsoft"
                                        }
                                    ],
                                    "term": "Microsoft",
                                    "type": "BCap"
                                }
                            ]
                        },
                        {
                            "count": 1,
                            "term": "Country",
                            "Country": [
                                {
                                    "region": "South Americas",

                                    "Brazil": [
                                        {
                                            "count": 1,
                                            "term": "Brazil"
                                        }
                                    ],
                                    "term": "Brazil",
                                    "currency": "Dollar (USD)"
                                }
                            ]
                        },
                         {"SalesRelated": [
                                {
                                    "count": 1,
                                    "term": "traffic"
                                }
                            ]
                        },
                        {
                            "count": 4,
                            "term": "Personality",
                            "Personality": [
                                {
                                    "count": 1,
                                    "term": "Maximor"
                                },

                                {
                                    "count": 1,
                                    "term": "R.V.P"
                                },
                                {
                                    "count": 1,
                                    "term": "Wenger"
                                },
                                {
                                    "count": 1,
                                    "term": "SAF"
                                }
                            ]
                        }
                    ]
                }
            ,
            "Link": "http://testLink.com/2"
         }]

http://jsbin.com/exuwet/3/edit


提示输入 如果字段选择= Country

预期产出:

YMFT112;    Country;    United States;  United States;      NA;         http://testLink.com/1;

YMFT112;    Country;    Canada;         Canada;             Toronto;    http://testLink.com/1;

YMFT113;    Country;    Brazil;         Brazil;             NA;         http://testLink.com/2;

如果字段选择= Company

预期产出:

YMFT112; Company;   Apple Inc;      Apple;      http://testLink.com/1;

YMFT113; Company;   Microsoft Corp; Microsoft;  http://testLink.com/2;

2 个答案:

答案 0 :(得分:1)

您可以在本机可用时使用JSON对象,或使用JSON2作为垫片。

之后,只需使用JavaScript的内置排序功能即可。您提供了一个与数组项相互比较的函数

var myArray = JSON.parse(jsonString);
myArray.sort(function(a, b){
    var nameA = a._source.Map.Company.term;
    var nameB = b._source.Map.Company.term;

    if (nameA === nameB) {
        return 0;
    } else if (nameA < nameB) {
        return -1
    }
    return 1;
});

答案 1 :(得分:0)

使用eval('(' + json_object + ')'),您将能够创建JavaScript对象。此对象将是一个数组,您可以使用.访问属性。 例如,如果您的json_object被称为数据,例如: 然后

    var temp = eval('(' + data + ')'); // temp now is an array.

如果您想要访问_index对象中的第一个idjson

    "_index": "test",
    "id": "YMFT112",

发出警报(temp[0]._index),它会显示"test"。对于其他属性,请遵循相同的逻辑。 This stackoverflow问题或JSON页面将帮助您了解在其他任务中您必须完成的任务才能完成任务。雅虎有一个名为YUI的API,可能会更有帮助。