0

In my controller I am fetching the json by using $http.get like this:

$http.get('http://webapp-app4car.rhcloud.com/product/feed.json').success(function(data)

now data is a JSON and I want to get the value corresponding to JSON key. My key is stored in a variable "key". How can I access the value ? data.key is not working as key is a variable

3 Answers 3

1

You can use object as arrays in javascript, try:

data[key];
Sign up to request clarification or add additional context in comments.

7 Comments

You mean you use the same syntax you'd use for an array. I strongly suspect data is not an array.
@DavidKnipe please learn about how bracket notation works in JavaScript before making incorrect remarks about it.
thanks, it is working and it is an object not an array @DavidKnipe
@Benjamin Please enlighten us.
@DavidKnipe In JavaScript you have two ways to access object properties. You can use the normal dot notation object.key and bracket notation object["key"] - in the second way (called bracket form) you access object keys by a dynamic string - so for example you can (but probably shouldn't) do something like: var x = {hello:"World"}; x["hell"+"o"] to get "World". In fact, even arrays are objects in JavaScript and even (in language specification, not practice) array keys are just string object keys that are coercible to UInt32 (arr["2"] and arr[2] are the same) es5.github.io/#x15.4
|
1

Use subscript notation.

data["key"];

Comments

0

Please create a fiddle for the same. Just a hunch, that your data is an array and hence you need to do something like data[0].key

Comments

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.