1

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I also think that structure is strange and he does not want it himself. I think was going to do something like this: "{'abc': {'url': 'abc.json'}}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.