从json对象获取值时出错

时间:2017-07-27 08:12:57

标签: json angular typescript

我从服务获得json响应我想从第一个块获取region_name属性值。 以下是我的json对象。

 SELECT * FROM profile WHERE name LIKE  'H%';

我的打字稿代码如下

[
{
    "blogid": 286,
    "post_title": "Family Time 2",
    "post_content": "",
    "post_date": "2017-07-27 06:26:34",
    "imageurl": "/wp-content/Love-market-banner.jpg",
    "region_name": "Asia",
    "cat_name": [
        {
            "term_id": 9,
            "name": "FAMILY TIME",
            "slug": "family-time",               
            "category_description": "this is family time",
            "cat_name": "FAMILY TIME",
            "category_nicename": "family-time",
            "category_parent": 0
        }
    ]
},
{
    "blogid": 285,
    "post_title": "Family Time 1",
    "post_content": "",
    "post_date": "2017-07-27 06:26:09",
    "imageurl": "/wp-content/reykjavik-xlarge.jpg",
    "region_name": "Asia",
    "cat_name": [
        {
            "term_id": 9,
            "name": "FAMILY TIME",
            "slug": "family-time",               
            "category_description": "this is family time",
            "cat_name": "FAMILY TIME",
            "category_nicename": "family-time",
            "category_parent": 0
        }
    ]
}
]

并在html中使用了

Blogbyregion(regid: any)
{
    var self = this;

    self.blogapi.BlogsbyRegion(regid).subscribe(
        x => {

            this.Blogbyreg = x;


        });
}

我收到结果但也收到错误

  

错误类型错误:无法读取null

的属性“0”

1 个答案:

答案 0 :(得分:1)

您应该初始化Blogbyreg字段Blogbyreg = [],或者null进行检查。