我正在使用 Javascript 通过 API 解码车辆 VIN 号。返回的消息如下所示:
{Count: 116, Message: "Results returned successfully", SearchCriteria: "VIN(s): 60540kjhkjhkj5885", Results: Array(1)}
我需要知道如何访问结果中返回的数组。
这是我当前的 Javascript:
// Decode VIN Number
function post_VIN_decode() {
var vinNumber = $('#id_vin_number').val();
console.log("VIN Decode Sent") // sanity check
$.ajax({
url : "https://vpic.nhtsa.dot.gov/api/vehicles/decodevinvalues/" + vinNumber + "?format=json", // the endpoint
type : "GET", // http method
// handle a successful response
success : function(json) {
console.log(json); // log the returned json to the console
},
// handle a non-successful response
error : function(xhr,errmsg,err) {
$('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
" <a href='#' class='close'>×</a></div>"); // add the error to the dom
console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
}
});
};
$('#id_vin_number').keyup(function(e){
if($("#id_vin_number").val().length >= 17){
event.preventDefault();
console.log("VIN Decode started") // sanity check
post_VIN_decode();
}
});
感谢任何帮助。
最佳答案
在你的成功尝试中:
success : function(json) {
console.log(json); // log the returned json to the console
$.each(json.Results, function( index, value ) {
console.log( index + ": " + value );
});
console.log("Or just the first index: " + json.Results[0]);
},
关于javascript - 访问 API 返回 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47341974/