访问json数组的语法

时间:2016-12-22 18:32:00

标签: javascript

我不知道访问" relatedTo"的语法。我的Json对象的数组。

当我JSON.parse(data).projects[0].name时,我有" New Pretender", 当我做JSON.parse(data).projects[0].relatedTo[0]我有未定义的

我尝试了几种不同的语法,但没有一种能够正常工作...... 结构是这样的:

{
    "projects": [{
        "name":"New Pretender", 
        "shortDescription":"A love coach not like the others", 
        "year":"2016",
        "description":"An interactive game/fiction design object you can play with a boob-shaped joystick. Talk about manipulation and other things.", 
        "relatedTo": ["ecriture", "rhetorique", "jeu"]
    }, {
        "name":"Pénombre", 
        "shortDescription":"A game about go out", 
        "year":"2016",
        "description":"This game invites th player to experiment what it is to be alone in a huge world, not knowing where you have to go.", 
        "relatedTo": ["jeu", "carte"]
    }, {
        "name":"Bodmer Lab", 
        "shortDescription":"A generative book based on Martin Bodmer' Faust collection", 
        "year":"2016",
        "description":"This is a book made with a lots of digitized old books that are in the Martin Bodmer huge collection.", 
        "relatedTo": ["ecriture", "generatif"]
    }]
}

2 个答案:

答案 0 :(得分:0)

鉴于上述结构,projects[0].relatedTo[0]将返回ecriture。我没有看到你的语法有任何不正确之处。但是,似乎你可能没有看到你认为自己的对象。

是否有可能projects[0]指向一个没有任何关系的项目(即空列表)?在尝试访问relatedTo[0]时,这将导致未定义。我们建议您查看relatedTo是什么,甚至projects[0]是什么。

答案 1 :(得分:0)

我觉得很愚蠢......我忘了我已经复制了我的json文件所以我只是调用了一个文件,其中该属性不存在。我改变了路径,现在没问题。