Ram
Ram

Reputation: 117

How do i get the specific value from json data using Angularjs

i am new to AngularJs. I am trying to get the values from json based on search key word and displaying that related data.

Below is my json format

{"abc":[  
    { "url":"abc.json"}  
 ],  
 "bbc":[   
    {"url":"bbc.json"}  
 ]
}

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.

Upvotes: 1

Views: 14440

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

In your JSON, '"abc"': is a key, and its corresponding value is

[{ "url":"abc.json"}]

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.

Upvotes: 2

Related Questions