2013-07-05 225 views
-7

我如何retrive從以下數組說明在javascript: 這裏的JS:如何遍歷json數組?

$(function() { 
    var description = data.output.description; 
    $("#myinput").suggest({ 
     "filter": "(all type:/automotive/model)", 
     "lang": "en", 
     "key": "***", 
     "output": "(description)", 
     'limit': 40, 
    }).bind("fb-select", function (e, data) { 
     $("#response2 pre").text(JSON.stringify(data, null, ' ')); 
     prettyPrint(); 
     $("#response2").show(); 
    }).bind("fb-select", function (e, data) { 
     $('#output').append(data.name + ", " + data.output['description']); 
    }); 
}); 

響應:

{ 
    "mid": "/m/03mnql", 
    "id": "/en/ford_f-series", 
    "name": "Ford F-Series", 
    "notable": { 
    "name": "Automobile Model", 
    "id": "/automotive/model" 
    }, 
    "lang": "en", 
    "score": 88.680244, 
    "output": { 
    "description": { 
     "wikipedia": [ 
     "The F-Series is a series of full-size pickup trucks from Ford Motor Company which has been sold continuously for over six decades. The most popular variant of the F-Series is the F-150. It was the best-selling vehicle in the United States for 24 years, currently the best-selling truck for 37 years, and the best selling vehicle in Canada, though this does not include combined sales of GM pickup trucks. In the tenth generation of the F-series, the F-250 and F-350 changed body style in 1998 and joined the Super Duty series.\nDuring the post-World War II era, smaller Canadian rural communities had access to either a Ford dealer or a Lincoln-Mercury-Meteor dealer, but not both; a Mercury-badged version was sold at Lincoln-Mercury-Meteor dealers there from 1946–68. Other than the grilles, trim, and badging, these trucks were identical to their Ford counterparts." 
     ] 
    } 
    } 
} 
+1

使用什麼編程語言? – chepner

+1

如果你使用JavaScript,那是一個對象字面值,而不是JSON。 –

+1

@chepner也許閱讀的問題:「在JavaScript中」。但我必須承認,「javascript」標籤確實缺失。 – 2013-07-05 18:59:58

回答

1
objectVariableName.output.description; 

這將返回一個對象與具有一個元素,它可能是你想要說明一個數組值的單個密鑰「維基百科」。

objectVariableName.output.description.wikipedia[0] 
1

如果這是一個對象字面:

var description = data.output.description; 

如果這仍然是JSON:

var description = JSON.parse(yourstring).output.description; 

但嚴重的是,它看起來並不像你做出了很大的努力。

+0

謝謝你的anwser,你非常正確,我想我沒有做出巨大的努力。 – vimes1984