2012-07-12 57 views
3

我正在试图以有序方式获取json中的项目。我能够选择json中存在的“term”值,但是可以按照我在预期的输出部分中显示的方式来安排它吗?我添加了一个的jsfiddle链接显示在那里我已经达到:将JavaScript对象转换为有序的逗号分隔值

[{ 
     "_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; 
+0

你试过'eval('('+ json_object +')')'?从那里创建对象不会成为问题。 – cybertextron 2012-07-12 12:46:12

+0

我没有尝试eval,实际上我看着它,我第一次处理josn字符串。 – user1371896 2012-07-12 12:48:26

+0

我发布了我的答案。请按照说明操作,我相信您可以为您的问题创建解决方案。 – cybertextron 2012-07-12 12:55:54

回答

0

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

var temp = eval('(' + data + ')'); // temp now is an array. 
如果你想从 json对象访问第一 _indexid

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

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

+0

让我检查它.. – user1371896 2012-07-12 12:56:05

+0

你有没有试过提醒我们我们的jsfiddle?它会ntrk – user1371896 2012-07-12 13:05:02

+0

@ user1371896我认为这个http://yuilibrary.com/yui/docs/json/正是你所需要的。 – cybertextron 2012-07-12 13:06:27

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; 
});