对于从AJAX调用返回的2维数组有点问题,不确定是否正确解析了JSON。
首先是AJAX调用:
$.ajax({
dataType: 'text',
url: "./inc/datadmin.php?action=getCust&id="+id,
success: function(data){
var client = JSON.parse(data);
for (var i = 0; i < client.cust.length; i++) {
var counter = client.cust[i];
console.log(counter.company_name);
alert('me');
}
)};
网络标签中显示的回报:
{
"cust":
{
"company_id":"11",
"company_name":"test",
"company_owner_name":"testo",
"company_owner_phone":"7038398608",
"company_phone":"7038398608",
"email":"test@test.com",
"ein":"EIN Test",
"mcin":"MCIN Test",
"dot_license":"DOT Test",
"ifta_license":"IFTA Test",
"ifta_exp_date":"2015-05-15",
"ky_ifta":"KY IFTA Test",
"ky_ifta_exp_date":"2015-05-13",
"nm_ifta":"NM IFTA Test",
"nm_ifta_exp_date":"2015-05-11",
"irp_license":"IRP Test",
"irp_exp_date":"2015-05-11",
"urc_license":"URC Test",
"urc_total_vehc":"URC TRUCK Test",
"urc_exp_date":"2015-05-23",
"fleet_one":"FleetOne Test"
},
"permits":
{
"company_id":"11",
"state":"IL",
"permit_number":"232",
"exp_date":"2015-05-28"
}
}
你可以看到数据看起来不错,格式正确,问题,它永远不会到达警报功能,基本上对我说client.cust.length的长度为0? 即使我不是高级程序员,我也可以看到循环应该至少有1条记录。
oops忘了添加数据库的回复:
$mdata = array();
$datas = $database->select("company", "*", array(
"companyId" => $_REQUEST['id']
));
foreach($datas as $data){
$mdata['cust']=array(
"company_id" => $data["companyId"],
"company_name" => $data["company_name"],
"company_owner_name" => $data["company_owner_name"],
"company_owner_phone" => $data["company_owner_phone"],
"company_phone" => $data["company_phone"],
"email" => $data["email"],
"ein" => $data["ein"],
"mcin" => $data["mcin"],
"dot_license" => $data["dot_license"],
"ifta_license" => $data["ifta_license"],
"ifta_exp_date" => $data["ifta_exp_date"],
"ky_ifta" => $data["ky_ifta"],
"ky_ifta_exp_date" => $data["ky_ifta_exp_date"],
"nm_ifta" => $data["nm_ifta"],
"nm_ifta_exp_date" => $data["nm_ifta_exp_date"],
"irp_license" => $data["irp_license"],
"irp_exp_date" => $data["irp_exp_date"],
"urc_license" => $data["urc_license"],
"urc_total_vehc" => $data["urc_total_vehc"],
"urc_exp_date" => $data["urc_exp_date"],
"fleet_one" => $data["fleet_one"],
);
}
$dataz = $database->select("permits", "*", array(
"companyId" => $_REQUEST['id']
));
foreach($dataz as $data){
$mdata['permits']=array(
"company_id" => $data["companyId"],
"state" => $data["state"],
"permit_number" => $data["permit_number"],
"exp_date" => $data["exp_date"]
);
}
echo json_encode($mdata);
所以&#34; cust&#34;部分是使用client.cust.company_id工作,但&#34;允许&#34;是的,我重新编写了数据库调用:
$dataz = $database->select("permits", "*", array(
"companyId" => $_REQUEST['id']
));
$mdata['permits']=array();
$xt=1;
foreach($dataz as $data){
$mdata['permits'][$xt]['company_id'] = $data["companyId"];
$mdata['permits'][$xt]['state'] = $data["state"];
$mdata['permits'][$xt]['permit_number'] = $data["permit_number"];
$mdata['permits'][$xt]['exp_date'] = $data["exp_date"];
$xt++;
}
数据回复如下:
"permits":
{
"1":
{"company_id":"11",
"state":"CA",
"permit_number":"ertr",
"exp_date":"2015-05-12"},
"2":
{"company_id":"11",
"state":"IL",
"permit_number":"232",
"exp_date":"2015-05-28"}
}
但我在alert(client.permits.length)中一直未定义; 所以它永远不会进入:
for (var i = 0; i < client.permits.length; i++) {
var counter = client.permits[i];
console.log(counter.state);
}
答案 0 :(得分:3)
with open("filename.pdf", "rb") as pdffile:
pdfdata=pdffile.read()
xmldata = scraperwiki.pdftoxml(pdfdata)
root = lxml.html.fromstring(xmldata)
是一个对象,而不是一个数组。该对象没有cust
属性,这就是永远不会输入循环的原因。
您可以直接访问length
上的媒体资源:
cust
如果要迭代对象的键,可以使用for...in
loop代替。
alert(client.cust.company_name);