求指导啊,一个JSON错误
这次做毕设遇到一个很怪异的问题啊,忘各位大侠能多多指导啊!
关于AJAX的,具体错误如下:
var zhichengsObj_ = xmlHttp.responseText;
var zhichengsJson = eval("(" + zhichengsObj_ + ")");
上面两行代码,通过火狐调试得知对象数据如下:
zhichengsObj_ = {"zhichengs":[{"ZHICHENGID":"1","ZHICHENGNAME":"高级会计师"},{"ZHICHENGID":"2","ZHICHENGNAME":"中级会计师"},{"ZHICHENGID":"3","ZHICHENGNAME":"高级经济师"},{"ZHICHENGID":"4","ZHICHENGNAME":"经济师"},{"ZHICHENGID":"5","ZHICHENGNAME":"助理经济师"},{"ZHICHENGID":"6","ZHICHENGNAME":"经济员"},{"ZHICHENGID":"7","ZHICHENGNAME":"人力资源师"},{"ZHICHENGID":"8","ZHICHENGNAME":"广告策划师"},{"ZHICHENGID":"9","ZHICHENGNAME":"普通员工"}]}
当代码运行到第二行时,报错:unterminated string litera
首先我以为是JSON对象有错,于是我做了个测试
var json = {"zhichengs":[{"ZHICHENGID":"1","ZHICHENGNAME":"高级会计师"},{"ZHICHENGID":"2","ZHICHENGNAME":"中级会计师"},{"ZHICHENGID":"3","ZHICHENGNAME":"高级经济师"},{"ZHICHENGID":"4","ZHICHENGNAME":"经济师"},{"ZHICHENGID":"5","ZHICHENGNAME":"助理经济师"},{"ZHICHENGID":"6","ZHICHENGNAME":"经济员"},{"ZHICHENGID":"7","ZHICHENGNAME":"人力资源师"},{"ZHICHENGID":"8","ZHICHENGNAME":"广告策划师"},{"ZHICHENGID":"9","ZHICHENGNAME":"普通员工"}]}
alert(json.zhichengs.length);
发现能弹出9这个数字,那不就说明传过来的json对象格式没错嘛!
于是我还不服输,可能是字符串转换成json对象出错,于是又做如下测试:
var str = '{"zhichengs":[{"ZHICHENGID":"1","ZHICHENGNAME":"高级会计师"},{"ZHICHENGID":"2","ZHICHENGNAME":"中级会计师"},{"ZHICHENGID":"3","ZHICHENGNAME":"高级经济师"},{"ZHICHENGID":"4","ZHICHENGNAME":"经济师"},{"ZHICHENGID":"5","ZHICHENGNAME":"助理经济师"},{"ZHICHENGID":"6","ZHICHENGNAME":"经济员"},{"ZHICHENGID":"7","ZHICHENGNAME":"人力资源师"},{"ZHICHENGID":"8","ZHICHENGNAME":"广告策划师"},{"ZHICHENGID":"9","ZHICHENGNAME":"普通员工"}]}';
//首先申明一个String对象
var json = eval("(" + str + ")");
alert(json.zhichengs.length);
发现这个字符串还是能转换成JSON对象,说明我传来的字符串也没有错啊,可是郁闷的是偏偏它就是出错了,忘大侠能多多指教啊,我实在想不出错误在哪儿啦!!!