How to loop through an JSON associative array in javascript?

前端 未结 6 731
遥遥无期
遥遥无期 2021-02-04 10:50

I\'m getting a JSON response from the server and i have to loop through the array in javascript and get the values. But I cant seem to loop throught it.

The JSON respons

6条回答
  •  一生所求
    2021-02-04 10:56

    You can use for-in construct in pure Javascript. Of course, you need to be careful that you're only looking at the object's own properties (libraries like Prototype tend to pollute):

    for(var key in response) {
        if(response.hasOwnProperty(key)) {
           ...
        }
    }
    

    EDIT

    Are you using jQuery.ajax? What's the dataType value? It should be json. This might be why your response is being interpreted as a string. Also, when you console.log response, does it show up as a string or an object?

提交回复
热议问题