javascript - 我正在尝试从 JSON 文件检索数据,但在从特定属性检索信息时遇到问题

标签 javascript arrays angularjs json object

这是我的 JSON 文件:

{
    "query":{
        "count":1,
        "created":"2016-06-16T21:09:11Z",
        "lang":"en-US",
        "results":{
            "channel":{
                "units":{
                    "distance":"mi",
                    "pressure":"in",
                    "speed":"mph",
                    "temperature":"F"
                },
                "title":"Yahoo! Weather - Ormond Beach, FL, US",
                "link":"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2466336/",
                "description":"Yahoo! Weather for Ormond Beach, FL, US",
                "language":"en-us",
                "lastBuildDate":"Thu, 16 Jun 2016 05:09 PM EDT",
                "ttl":"60",
                "location":{
                    "city":"Ormond Beach",
                    "country":"United States",
                    "region":" FL"
                }
            }
        }
    }
}

这不是全部代码,但这是我遇到问题的部分。

我正在尝试将位置(佛罗里达州奥蒙德海滩)保存为变量。我正在使用这个 angularjs 代码,但它似乎不起作用:$scope.location = response.data.query.results.location;。 JSON 文件没有任何问题,因为我稍后使用 ng-repeat 在 JSON 文件中检索了其他信息,但是,我在将 Ormond Beach 保存为变量$scope.location。请帮我!

非常感谢!

最佳答案

位置和结果之间有一个中间对象,即 channel 。

尝试一下:

$scope.location = response.data.query.results.channel.location;

然后你只需要连接location.city和location.region

关于javascript - 我正在尝试从 JSON 文件检索数据,但在从特定属性检索信息时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37869907/

相关文章:

在 C 中比较两个 char* 和两个 char[ ] 字符串

c++ - 如何将未知大小的二维矩阵发送给函数

c++ - 将 std::vector 与 MPI_Gather 一起使用会崩溃,而 c 样式数组工作正常

javascript - Angular : Regex nothing to repeat error

javascript - 如果我尝试访问一个未定义或继承的属性,它返回空字符串。为什么会有这样的行为?

javascript - 使用 Array.reduce() 将数组转换为对象

javascript - 输入文本的动画在 Firefox 中不起作用

javascript - 如何在 ng-repeat 表中查找单选按钮值?

javascript - 如何正确使用 ng-cloak 指令?

javascript - ReactJS 通过 props 回调设置另一个类的状态