我有一个包含JSON值的变量。现在我需要从JSON获取每个值并将其绑定到相应的HTML元素。以下是我的代码:
var sess = sessionStorage.getItem('session');
alert(sess['EmpName']);
我刚刚在警报中获得未定义。即使我也尝试过如下,
var sess = sessionStorage.getItem('session');
var ses = JSON.parse(sess);
alert(ses.EmpName);
为此,我得到未定义。如何从JSON获取值并使用angular2在HTML元素中绑定相同的值。
我通过console.log(JSON.parse(sessionStorage.getItem('session')));
下面是我得到的。[{"CompanyCode":1,"EmpSKNo":111,"EmpNumber":"sf","EmpName":"sdfdsf K","DataScope":null,"ProgramName":"Cons - ","ProgramCode":56345,"ProgramScope":"51","DesignationCode":"756","Designation":"dfgg ","GradeCode":34,"DesignationLevel":"100","LocationCode":154,"LocationName":"ere","LocationScope":"9C.1","UserCode":"sdf","AliasFlag":"0","GroupCode":0,"GroupName":null,"NTId":"df","EmpVersion":54,"MngrSKNo":"434","EmailId":"sgfsg.K@sdfdf.COM","EMPGEO":"IND01","EMPMANAGERPKID":"434","EMPLOYEEPKID":"5345","EMPLEVEL":"162012.26.20"}]
答案 0 :(得分:0)
您需要解析为Object
var sess = JSON.parse(sessionStorage.getItem('session'));
alert(sess['EmpName']);
正如我在评论中提到的,所以你需要这个。
var sess = sessionStorage.getItem('session');
var ses = JSON.parse(sess);
alert(ses[0].EmpName);