JSON对象返回值但显示未定义 [英] JSON object returning with values but displaying undefined

查看:159
本文介绍了JSON对象返回值但显示未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据库中选择数据并将其返回到json对象中,但是当我尝试访问对象的属性时,它们显示为undefined,但是如果我查看返回的对象,则使用Mozilla的firebug或内置的Chromes web开发工具,对象的属性有值,

I'm selecting data from a database and returning it in a json object, but when i try to access the object's properties they display undefined, but if i look at the object being returned, either using Mozilla's firebug or Chromes built in web dev tools, the object's properties have values,

$.getJSON("info.php", {uid:one}, function(data){
        var size = $(data).size();
        console.log(data);
        //display systems
        $(child).append('<tr><th height="20" colspan="2">Contact Person</th><th height="20">Contact Number</th><th height="20" colspan="2">E-mail</th></tr><tr><td colspan="2">'+data.contact_person+'</td><td>0'+data.contact_number+'</td><td colspan="2">'+data.email_address+'</td></tr>');

        child.show();
        loader.hide();

    });

非常感谢任何帮助。

以下是chrom如何显示它(我用-----替换敏感数据):

Here's how chrom displays it (i've replaced sensitive data with "-----"):

[
Object

bee:undefined

bwo:undefined

cell_number:---

city:---

company_name:----

contact_number:----

contact_person:


date:2010-10- 18

email_address:-----

esco_number:-------


fax_number:0

fgn:undefined

lbs:undefined

none:undefined

数字:761

其他:undefined

pobox:------

postal_city:------

postal_code :-----

postal_postal_code:-----

postal_province:---------

postal_suburb:--------

省:-------


same_as_physical:


smee :undefined

状态:有效

街道:--------

郊区:----- -----结果
时间:11:36:21

uid:----------

网站:


proto :对象

]

[ Object
bee: "undefined"
bwo: "undefined"
cell_number: "---"
city: "---"
company_name: "----"
contact_number: "----"
contact_person: ""
date: "2010-10-18"
email_address: "-----"
esco_number: "-------"
fax_number: "0"
fgn: "undefined"
lbs: "undefined"
none: "undefined"
number: "761"
other: "undefined"
pobox: "------"
postal_city: "------"
postal_code: "-----"
postal_postal_code: "-----"
postal_province: "---------"
postal_suburb: "--------"
province: "-------"
same_as_physical: ""
smee: "undefined"
status: "Active"
street: "--------"
suburb: "----------"
time: "11:36:21"
uid: "----------"
website: ""
proto: Object
]

//尝试访问chrome控制台中的数据

Object.pobox

undefined

// trying to access the data in chrome's console
Object.pobox
undefined

//这里是console.dir:

数组[1]

0:对象

bee:

bwo:


cell_number:123456789

city: Bathurst

company_name:test

contact_number:123456789

contact_person:test

date:2011- 03-22

email_address:test@test.test

esco_number:010101

fax_number:123456789

fgn:


lbs:

无:


数字:01

其他:

pobox:0

postal_city:


postal_code:0

postal_postal_code:0

postal_province:


postal_suburb:


省份:东开普省

same_as_physical:on

smee:

状态:有效< br>
街道:测试

郊区:测试

时间:10:09:04

uid:0

网站:test.co.za

proto :对象

长度:1

proto :数组[0]

// here's console.dir:
Array[1]
0: Object
bee: ""
bwo: ""
cell_number: "123456789"
city: "Bathurst"
company_name: "test"
contact_number: "123456789"
contact_person: "test"
date: "2011-03-22"
email_address: "test@test.test"
esco_number: "010101"
fax_number: "123456789"
fgn: ""
lbs: ""
none: ""
number: "01"
other: ""
pobox: "0"
postal_city: ""
postal_code: "0"
postal_postal_code: "0"
postal_province: ""
postal_suburb: ""
province: "Eastern Cape"
same_as_physical: "on"
smee: ""
status: "Active"
street: "test"
suburb: "test"
time: "10:09:04"
uid: "0"
website: "test.co.za"
proto: Object
length: 1
proto: Array[0]

推荐答案

啊,看起来你正在获得一个数组其中的一个对象。你可以在你的PHP脚本中修复它,或者只是在javascript函数的开头添加一行修复,

Ahh, looks like you're getting an array with an object in it. You can either fix it in your PHP script or just add a one-line fix at the beginning of the javascript function,

data = data[0];

这篇关于JSON对象返回值但显示未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆