Loop and get key/value pair for JSON array using jQuery

后端 未结 6 548
醉话见心
醉话见心 2020-11-29 23:31

I\'m looking to loop through a JSON array and display the key and value.

It should be a simplified version of the following post, but I don\'t seem to have the syn

6条回答
  •  醉梦人生
    2020-11-30 00:23

    You have a string representing a JSON serialized JavaScript object. You need to deserialize it back to a JavaScript object before being able to loop through its properties. Otherwise you will be looping through each individual character of this string.

    var resultJSON = '{"FirstName":"John","LastName":"Doe","Email":"johndoe@johndoe.com","Phone":"123 dead drive"}';
    var result = $.parseJSON(resultJSON);
    $.each(result, function(k, v) {
        //display the key and value pair
        alert(k + ' is ' + v);
    });
    

    Live demo.

提交回复
热议问题