Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
访问JavaScript JSON中的对象属性_Javascript_Json_Object - Fatal编程技术网

访问JavaScript JSON中的对象属性

访问JavaScript JSON中的对象属性,javascript,json,object,Javascript,Json,Object,我正在做一些基本的对象项目来使用它们,同时也在使用JSON。然而,当我试图访问嵌套属性时,我一直被返回“未定义”,因此我遗漏了一些东西 目标是创建一个新的场景对象,并根据我传递的场景名称加载内容 function newScene(name) { var sceneName = name; this.sceneDetails = function() { var theDetails = { "home": [

我正在做一些基本的对象项目来使用它们,同时也在使用JSON。然而,当我试图访问嵌套属性时,我一直被返回“未定义”,因此我遗漏了一些东西

目标是创建一个新的场景对象,并根据我传递的场景名称加载内容

function newScene(name) {
    var sceneName = name;

    this.sceneDetails = function() {

        var theDetails = {
            "home": [
                { "title": "Home Page",
                "desc": "This home page is the homiest of all home pages." }
            ],
            "about": [
                { "title": "The About Page",
                "desc": "This page is about stuff." }
            ]
        }
        console.log(theDetails[sceneName]['title']);
    }
}


var thePage = new newScene('home');
var thePageBuild = thePage.sceneDetails();

当运行这个时,我会被赋予“未定义”的头衔,而不是真正的主场头衔。任何帮助都将不胜感激。谢谢!

您正在向数组添加标题和说明,因此实际详细信息位于

console.log(theDetails[sceneName][0]['title']);
我怀疑这不是你想要的,所以将细节分配改为这,代码应该可以正常工作:

var theDetails = {
    "home": { 
        "title": "Home Page",
        "desc": "This home page is the homiest of all home pages."
    },
    "about": { 
        "title": "The About Page",
        "desc": "This page is about stuff." 
    }            
}
试试这个

function newScene(name) {
    var sceneName = name;

    this.sceneDetails = function() {

        var theDetails = {
            "home": [
                { "title": "Home Page",
                "desc": "This home page is the homiest of all home pages." }
            ],
            "about": [
                { "title": "The About Page",
                "desc": "This page is about stuff." }
            ]
        }
        console.log(theDetails[sceneName][0].title);
    }
}


var thePage = new newScene('home');
var thePageBuild = thePage.sceneDetails();

这是正在工作的

因为您已将home和about设置为阵列,所以需要按如下方式访问它们:

console.log(theDetails[sceneName][0]['title']);
   var theDetails = {
        "home": 
            { "title": "Home Page",
            "desc": "This home page is the homiest of all home pages." },
        "about": 
            { "title": "The About Page",
            "desc": "This page is about stuff." }

    }
或者像这样重新构造json:

console.log(theDetails[sceneName][0]['title']);
   var theDetails = {
        "home": 
            { "title": "Home Page",
            "desc": "This home page is the homiest of all home pages." },
        "about": 
            { "title": "The About Page",
            "desc": "This page is about stuff." }

    }

再次检查您的结构,您有一个数组,您必须通过索引访问它,然后访问对象。将日志行更改为
console.log(详细信息[sceneName][0]['title'])正如@elclanrs指出的,场景对象位于数组中。我假设您只想删除场景对象周围的
[]
。然后它将按您预期的方式运行。在这种情况下,您可以通过详细信息[sceneName][0]进行访问。标题。最好是完全消除数组,然后可以使用原始调用。此外,您的结构没有多大意义。var’theDetails不应该出现在你的函数中……约翰·斯特林是第一个,回答完全相同,很有趣,因为那家伙也抄袭了我的一个答案:)