javascript - 访问 API 返回 JSON 数组

标签 javascript json

我正在使用 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'>&times;</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/

相关文章:

java - com.sun.jersey.api.client.ClientHandlerException : java. io.IOException:过早的 EOF

javascript - 映射对象数组,然后映射这些对象中的另一个数组并消除重复项

javascript示例显示内容框

iPhone、核心数据和 JSON

python - 如何将 Twitter json 对象加载到 python 中

python - 如何使用 simplejson 验证 JSON

json - 如何根据其他值筛选使用 jq 选择的对象?

javascript - 按名称获取嵌套元素

javascript - React 按名称数组获取选择值

javascript - 对动态内容多次使用相同的模态代码