Parsing jQuery data in JavaScript (JSON)

前端 未结 2 1730
一个人的身影
一个人的身影 2021-01-28 12:29

I am using PHP, jQuery, and JSON. Now I need to know how to parse the jQuery data in JavaScript.

load.php



        
相关标签:
2条回答
  • 2021-01-28 12:52

    You have an array of JSON objects, so you need to loop through the array to get each individual object:

    for(var x = 0; x < myjson.length; x++) {
        alert(myjson[x].name);
        // myjson[x].distance, myjson[x].code also available
    }
    

    Or, if you want to do it the jQuery way:

    jQuery.each(myjson, function(x) {
        alert(myjson[x].name);
    });
    

    Both examples will give you an alert with 'STA' followed by 'GIS'.

    In addition to this, as pointed out by OIS, you're trying to read the wrong variable in your code. The JSON should be in variable named a.

    0 讨论(0)
  • 2021-01-28 12:56

    You try to use a variable named json which is not defined in the scope of your function. Instead you have to use the argument named a.

    function pass(a) {
       var i;
        while(i = a.pop()) {
            alert(i.name + " " + i.distance);
        }
    }
    
    0 讨论(0)
提交回复
热议问题