我要如何使用Angularjs JSON数据的特定值 [英] How do i get the specific value from json data using Angularjs
问题描述
我是新来AngularJs。我想基于搜索的关键字从JSON获取值并显示相关的数据。
i am new to AngularJs. I am trying to get the values from json based on search key word and displaying that related data.
下面是我的JSON格式
Below is my json format
{"abc":[
{ "url":"abc.json"}
],
"bbc":[
{"url":"bbc.json"}
]
}
我正在用比较输入的搜索词的关键。如果匹配的话,我需要加载单独的JSON这是关系到找到钥匙。我能够搜索词与主要的比较,但是当我试图获得的价值正在逐渐像URL = abc.json总价值,而不是这个,我只得到abc.json。我的控制器code是如下:
i am comparing the key with entered search word. If it matches then i need to load the separate json which is related to found key. I am able to compare the search word with key, but when i am trying to get the value am getting total value like url=abc.json, instead of this i've to get only abc.json. My controller code is as follows
function getDetailsController($scope,$http) {
$scope.search = function() {
$http.get("searchInfo.json").success(function(response) {
angular.forEach(response, function(value, key) {
if(angular.equals(key, $scope.searchText)){
$scope.url = value;
$http.get(url).success(function(response) {
$scope.details = response;
});
}
});
});
};
}
我曾尝试在不同势方法,但我没能拿到价值。你能帮我在此。
I have tried in diffrent ways but i was not able to get the value. Could you please help me on this.
推荐答案
在您的JSON,ABC':是一个关键,其对应值为
In your JSON, '"abc"': is a key, and its corresponding value is
[{ "url":"abc.json"}]
这是包含单个对象的数组,包含一个键的对象:URL
That is an array containing a single object, the object containing a single key: "url"
.
因此,访问此网址,您只需
So, to access this URL, you simply need
value[0].url
这结构很奇怪,但。我真的不明白包装内部数组对象的点。
This structure is quite strange though. I don't really understand the point of wrapping the object inside an array.
这篇关于我要如何使用Angularjs JSON数据的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!