【问题标题】:One part of JSON data being interpreted as an object and an similar part being interpreted as an array: why?JSON 数据的一部分被解释为对象,而类似的部分被解释为数组:为什么?
【发布时间】: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


【解决方案1】:

employjobs 在某些情况下可能为空。如果它永远不应该是空的,你需要调查一下。如果它为空有效,php 默认会省略 JSON 的空数组语法。如果这是不可取的(即您想要一个空对象),您可以为json_encode 使用JSON_FORCE_OBJECT 选项:

echo json_encode($data, JSON_FORCE_OBJECT);

这将被省略为

'{"labels": "object with various values", "employjobs": {}}'

【讨论】:

  • 谢谢 - 这可以让employjobs 成为一个对象。现在我只需要弄清楚为什么当我在浏览器中打开 php 脚本时,尽管 JSON 字符串有两个条目,但该对象是空的。非常感谢您的帮助。
猜你喜欢
  • 2022-01-21
  • 1970-01-01
  • 2011-02-22
  • 1970-01-01
  • 2021-12-09
  • 2010-12-20
  • 1970-01-01
  • 2020-01-04
  • 2021-03-26
相关资源
最近更新 更多