简体   繁体   中英

Read & parse JSON response from API

I have an API endpoint like this: https://client.systemonesoftware.com/bannink/json/?language=nl

I need to read and parse it into a table with all the information. But I get no output when I try to do it with Javascript.

<script>
    $.getJSON('https://client.systemonesoftware.com/bannink/json/?language=nl', function(data) {
        var json = JSON.parse(data);

        alert(json.cached);
        alert(json.data[1].id);
    });
    </script>

This piece of code gives nothing..

$.getJSON returns JavaScript object so you don't need to parse it, try:

 $.getJSON('https://client.systemonesoftware.com/bannink/json/?language=nl', function(json) { console.log(json.data[1].id); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>

The success callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method. It is also passed the text status of the response.

I believe your issue is that the data already comes through parsed:

<script>
$.getJSON('https://client.systemonesoftware.com/bannink/json/?language=nl', function(json) {
    alert(json.cached);
    alert(json.data[1].id);
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM