以json数据格式访问数组对象

时间:2014-01-23 01:05:16

标签: javascript json

postItem : [{
    id: 1,
    content: 'string',
    date: '1 Jan 2014',
    category: '1'

    },

    {
    id: 2,
    content: 'string2',
    date: '14 Jan 2014',
    category: '2'

    }]

postItem [0] .content无法选择第一个数组的内容对象。我做错了吗?

1 个答案:

答案 0 :(得分:1)

您必须看到postItem也是一个属性。假设您在JSON数据中只返回了一个对象,即data = {postItem, ... ,},那么要访问postItem本身,您必须引用它所属的对象(在这种情况下是数据)。

因此,如果您执行postItem[0].content,则会收到错误,因为该范围内不存在postItem。现在,如果您执行data.postItem[0].content,那么您将获得正确的,因为您正在访问它确实存在的变量。

相关问题