简体   繁体   English

未捕获的TypeError无法读取json数据未定义的属性'image'

[英]Uncaught typeerror cannot read property 'image' of undefined with json data

I'm having an 我正在

Uncaught TypeError cannot read property 'image' of undefined

error when calling some Json data. 调用某些Json数据时出错。 My js is the following: 我的js如下:

$.getJSON('/js/data.json', function (json) {
    Object.keys(json).forEach(function (country) {
        $(".marker." + country.toLowerCase()).on("click", function () {
            $("#show").html(
                "Image: <img src=" + json[country][0].image + ">" +
                "| Description: <h1>" + json[country][0].description) + "</h1>";
        });
    });
});

and my data.json the following: 和我的data.json如下:

{
    "france": {
        "image": "img/ausralia.jpg",
        "description": "number django 1"
    },
    "australia": {
        "image": "img/ausralia.jpg",
        "description": "number django 2"
    },
    "uk": {
        "image": "img/ausralia.jpg",
        "description": "number django 3"
    }
}

ON click, no data are being display and I'm having that error being display in my console. 单击时,没有数据正在显示,并且我的控制台中正在显示该错误。

The weird thing is that If i call the data like this in my js, the error doesn't appear and everything works correctly. 奇怪的是,如果我在js中调用像这样的数据,则不会出现错误,并且一切正常。 But I need to be able to call the data from an external file. 但是我需要能够从外部文件中调用数据。

var json = {
    "France": [{
        "image": "img/ausralia.jpg",
        "description": "number django 1"
    }],
    "Australia": [{
        "image": "img/ausralia.jpg",
        "description": "number django 2"
    }],
    "uk": [{
        "image": "img/ausralia.jpg",
        "description": "number django 2ee"
    }]
}

Object.keys(json).forEach(function (country) {
    $(".marker." + country.toLowerCase()).on("click", function () {
        $("#show").html(
            "Image: <img src=" + json[country][0].image + ">" +
            "| Description: <h1>" + json[country][0].description) + "</h1>";
    });
});

Any help will be amazing, I know there is post about this error, but I cant find any solution to make it work ! 任何帮助都将是惊人的,我知道有关于此错误的帖子,但是我找不到任何解决方案来使其正常工作!

Hope it make sense , thanks for your time ! 希望有道理,感谢您的宝贵时间!

Try this, 尝试这个,

Object.keys(json).forEach(function (country) {

    if(typeof json[country][0].image !== "undefined" && typeof json[country][0].description !== "undefined" ) {

        $(".marker." + country.toLowerCase()).on("click", function () {

            $("#show").html( "Image: <img src=" + json[country][0].image + ">" + "| Description: <h1>" + json[country][0].description) + "</h1>";

        });

    }

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JSON-未捕获的TypeError:无法读取未定义的属性 - JSON - Uncaught TypeError: Cannot read property of undefined 未捕获的类型错误:无法读取未定义 JSON 的属性 - Uncaught TypeError: Cannot read property of undefined JSON JSON TypeError:无法读取未定义的属性“ image” - JSON TypeError: Cannot read property 'image' of undefined json出现Uncaught TypeError错误:无法读取未定义的属性“选择” - Error with json Uncaught TypeError: Cannot read property 'choices' of undefined d3.json:“未捕获的TypeError:无法读取未定义的属性&#39;children&#39;” - d3.json: “Uncaught TypeError: Cannot read property 'children' of undefined” 未捕获的类型错误:无法读取简单 JSON 数组的未定义属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined for simple JSON array 未捕获(承诺中)类型错误:无法读取未定义的属性“json” - Uncaught (in promise) TypeError: Cannot read property 'json' of undefined 未捕获的TypeError:无法读取json_encode()上未定义的属性“ length” - Uncaught TypeError: Cannot read property 'length' of undefined on json_encode() Jquery json:未捕获TypeError:无法读取未定义的属性&#39;xx&#39; - Jquery json : Uncaught TypeError: Cannot read property 'xx' of undefined JSON JQUERY未捕获的TypeError:无法读取未定义的属性“ length” - JSON JQUERY Uncaught TypeError: Cannot read property 'length' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM