How to loop through JSON array?

前端 未结 8 1635
鱼传尺愫
鱼传尺愫 2020-12-01 14:52

I have some JSON-code which has multiple objects in it:

[
    {
        \"MNGR_NAME\": \"Mark\",
        \"MGR_ID\": \"M44\",
        \"EMP_ID\": \"1849\"
           


        
相关标签:
8条回答
  • 2020-12-01 15:44

    You need to access the result object on iteration.

    for (var key in result)
    {
       if (result.hasOwnProperty(key))
       {
          // here you have access to
          var MNGR_NAME = result[key].MNGR_NAME;
          var MGR_ID = result[key].MGR_ID;
       }
    }
    
    0 讨论(0)
  • 2020-12-01 15:44

    You can iterate over the collection and check each object if it contains the property:

    var count = 0;
    var i;
    for(i = 0; i < jsonObj.length; i += 1) {
        if(jsonObj[i]["MNGR_NAME"]) {
            count++;
        }
    }
    

    Working example: http://jsfiddle.net/j3fbQ/

    0 讨论(0)
提交回复
热议问题