2016-08-15 8 views
0

次のJSONデータからpeople_id,username,nameおよびpeople_typeを検索したいと考えています。最終的には、これらの詳細をブートストラップ・モーダル・テーブルに移入する必要があります。 は、私は詳細を取得するために使用されているjQueryのコードで以下:私はnamepeople_type .Hereにアクセスしようとしていますネストされたjsonにアクセスする際に未定義になる

.done(function (data_, textStatus_, jqXHR_) { 
$.each(data_.people_list, function(key, value) { 

        $.each(value.details_list, function(k,v) { 
           console.log("Inside Loop:"+v.name); 
           $.each(v.role, function(k1,v1) { 
           console.log("Inside Another Loop for Role Name:"+v1.people_type); 
           }); 
        }); 
       }); 

}) 

を現時点では私は私のコンソール上で取得しています出力されます。

Inside Loop:First Hand Details 
Inside Another Loop for Role Name:undefined 
Inside Loop:First Hand Details 
Inside Another Loop for Role Name:undefined 
Inside Loop:UAB Multiple Myeloma Registry 
Inside Another Loop for Role Name:undefined 
Inside Loop:First Hand Details 
Inside Another Loop for Role Name:undefined 
Inside Loop:First Hand Details 
Inside Another Loop for Role Name:undefined 
Inside Loop:First Hand Details 
Inside Another Loop for Role Name:undefined 

なぜpeople_typeが未定義ですか?その特定のものが存在しないときに私はundefinedを得ることをオンラインで読むのですか?それは.each(で利用できるよう

{ 
    "webservice_status" : { 
    "status" : "SUCCESS", 
    "message" : "" 
    }, 
    "people_list" : [ { 
    "people_id" : 001, 
    "username" : "pxy123", 
    "full_name" : "James Camerion", 
    "details_list" : [ { 
     "id" : 1234, 
     "name" : "First Hand Details", 
     "role" : { 
     "type_id" : 3042, 
     "people_type" : "SuperUser" 
     } 
    } ] 
    }, { 
    "people_id" : 002, 
    "username" : "gbt123", 
    "full_name" : "Tom Cruise", 
    "details_list" : [ { 
     "id" : 1234, 
     "name" : "First Hand Details", 
     "role" : { 
     "type_id" : 3041, 
     "people_type" : "Normal User" 
     } 
    }, { 
     "id" : 5678, 
     "name" : "Second Hand Details", 
     "role" : { 
     "type_id" : 3042, 
     "people_type" : "SuperUser" 
     } 
    } ] 
    }, { 
    "people_id" : 003, 
    "username" : "txt456", 
    "full_name" : "Michael Jordon", 
    "details_list" : [ { 
     "id" : 1234,                          
     "name" : "First Hand Details", 
     "role" : { 
     "type_id" : 3042, 
     "people_type" : "SuperUser" 
     } 
    } ] 
    }, { 
    "people_id" : 004, 
    "username" : "mxn788", 
    "full_name" : "Paul Walker", 
    "details_list" : [ { 
     "id" : 1234, 
     "name" : "First Hand Details", 
     "role" : { 
     "type_id" : 3042, 
     "people_type" : "SuperUser" 
     } 
    } ] 
    }, { 
    "people_id" : 005, 
    "username" : "nhgy234", 
    "full_name" : "Brad Pitt", 
    "details_list" : [ { 
     "id" : 1234, 
     "name" : "First Hand Details", 
     "role" : { 
     "type_id" : 3042, 
     "people_type" : "SuperUser" 
     } 
    } ] 
    } ] 
} 
+0

ロールは配列ではなく、ループする必要はありません – aw04

+1

同じ「レベル」で「v.name」にアクセスすると、 'v.role.people_type'にアクセスできます。 –

答えて

1

は、オプション
ループvに不要以下の結果を使用することを期待を取得するために)、それは単一のオブジェクトであり、複数のオブジェクト

$.each(test[0].people_list, function(key, value) { 

        $.each(value.details_list, function(k,v) { 

           console.log("Inside Loop:"+v.name); 

           console.log("Inside Another Loop for Role Name:"+v.role.people_type); 

        }); 
       }); 




var test =[{ 
    "webservice_status" : { 
    "status" : "SUCCESS", 
    "message" : "" 
    }, 
    "people_list" : [ { 
    "people_id" : 001, 
    "username" : "pxy123", 
    "full_name" : "James Camerion", 
    "details_list" : [ { 
     "id" : 1234, 
     "name" : "First Hand Details", 
     "role" : { 
     "type_id" : 3042, 
     "people_type" : "SuperUser" 
     } 
    } ] 
    }, { 
    "people_id" : 002, 
    "username" : "gbt123", 
    "full_name" : "Tom Cruise", 
    "details_list" : [ { 
     "id" : 1234, 
     "name" : "First Hand Details", 
     "role" : { 
     "type_id" : 3041, 
     "people_type" : "Normal User" 
     } 
    }, { 
     "id" : 5678, 
     "name" : "Second Hand Details", 
     "role" : { 
     "type_id" : 3042, 
     "people_type" : "SuperUser" 
     } 
    } ] 
    }, { 
    "people_id" : 003, 
    "username" : "txt456", 
    "full_name" : "Michael Jordon", 
    "details_list" : [ { 
     "id" : 1234,                          
     "name" : "First Hand Details", 
     "role" : { 
     "type_id" : 3042, 
     "people_type" : "SuperUser" 
     } 
    } ] 
    }, { 
    "people_id" : 004, 
    "username" : "mxn788", 
    "full_name" : "Paul Walker", 
    "details_list" : [ { 
     "id" : 1234, 
     "name" : "First Hand Details", 
     "role" : { 
     "type_id" : 3042, 
     "people_type" : "SuperUser" 
     } 
    } ] 
    }, { 
    "people_id" : 005, 
    "username" : "nhgy234", 
    "full_name" : "Brad Pitt", 
    "details_list" : [ { 
     "id" : 1234, 
     "name" : "First Hand Details", 
     "role" : { 
     "type_id" : 3042, 
     "people_type" : "SuperUser" 
     } 
    } ] 
    } ] 
}]; 


$.each(test[0].people_list, function(key, value) { 

        $.each(value.details_list, function(k,v) { 

           console.log("Inside Loop:"+v.name); 

           console.log("Inside Another Loop for Role Name:"+v.role.people_type); 

        }); 
       }); 

Codepenを持っていません: http://codepen.io/nagasai/pen/BzrEVN

出力:

Inside Loop:First Hand Details 
Inside Another Loop for Role Name:SuperUser 
Inside Loop:First Hand Details 
Inside Another Loop for Role Name:Normal User 
Inside Loop:Second Hand Details 
Inside Another Loop for Role Name:SuperUser 
Inside Loop:First Hand Details 
Inside Another Loop for Role Name:SuperUser 
Inside Loop:First Hand Details 
Inside Another Loop for Role Name:SuperUser 
Inside Loop:First Hand Details 
Inside Another Loop for Role Name:SuperUser 
関連する問題