【发布时间】:2013-07-08 06:08:17
【问题描述】:
我有一个输出 text/json 的 php 脚本,输出如下:
{"labels":{"ftemp":"Full time employment only","ptemp":"Part time employment only","study":"Further study only","workstudy":"Work and Study","noavail":"Not available for work","noemp":"Unemployed","other":"Other","refusal":"Information Refused"},"employjobs":{"Cambridge Beds Co Ltd.":"Accounts Assistant","Chinese Company":"Accountant"}}
格式更好,看起来像这样:
{
"labels":
{
"ftemp":"Full time employment only",
"ptemp":"Part time employment only",
"study":"Further study only",
"workstudy":"Work and Study",
"noavail":"Not available for work",
"noemp":"Unemployed",
"other":"Other",
"refusal":"Information Refused"
},
"employjobs":
{
"Cambridge Beds Co Ltd.":"Accounts Assistant",
"Chinese Company":"Accountant"
}
}
现在,对我来说,'labels' 和 'employjobs' 看起来都是带有键值对的 json 对象。但是,当我对脚本进行 JQuery getJSON 调用时,在返回的数据对象中,“labels”是一个对象,但“employjobs”是一个空数组。
我错过了什么? json 字符串的两个位的格式看起来都一样,为什么一个被解释为对象而另一个被解释为空数组?
非常感谢您的任何帮助,在此先感谢您。
更新: 这是 PHP 脚本中的数据在通过 json_encode 函数之前的 print_r 输出:
Array
(
[labels] => Array
(
[ftemp] => Full time employment only
[ptemp] => Part time employment only
[study] => Further study only
[workstudy] => Work and Study
[noavail] => Not available for work
[noemp] => Unemployed
[other] => Other
[refusal] => Information Refused
)
[employjobs] => Array
(
[Cambridge Beds Co Ltd.] => Accounts Assistant
[Chinese Company] => Accountant
)
)
如您所见,'labels' 和 'employjobs' 都是键值对数组,这反映在 PHP 脚本输出的 JSON 字符串中。
【问题讨论】:
-
是否尝试在控制台中查看 JSON 对象?
-
是的,这就是我用来检查对象的。
-
这是 WebKit Inspector 中控制台的输出:
Object {labels: Object, employjobs: Array[0]} employjobs: Array[0] length: 0 __proto__: Array[0] labels: Object ftemp: "Full time employment only" noavail: "Not available for work" noemp: "Unemployed" other: "Other" ptemp: "Part time employment only" refusal: "Information Refused" study: "Further study only" workstudy: "Work and Study" __proto__: Object __proto__: Object -
一定是php端报错,重新检查你的php代码
-
谢谢,我会再次检查PHP。但是,上面的 JSON 字符串绝对是脚本输出的内容,因为我直接在浏览器中打开了脚本 URL,并且填充了employjobs 对象,这让我很困惑。如果 JSON 字符串确实包含名称值对,为什么它们没有被拾取?感谢您的帮助,谢谢。
标签: php javascript jquery json