0

After an $http get request resource api gives back a multyline json:(example)

{"features":["type": "feature", "properties":{ "id": "001", "name": "Example"}]}

How can i get an id from that data?

$scope.myDate = $http.get('http://example.com/').
  success(function(data, status){
    var test = data.features;
    $scope.information = test['type'];

  }).
  error(function(data, status){
    console.log('not');
  });

The way i used isn't correct, so any suggestions? Thank you, in advance!

1
  • 1
    this json seem not valid, features is an array, and contain key-values. Should be an object Commented Nov 20, 2014 at 10:08

1 Answer 1

1

Your JSON is not valid, you need to change it as follows and then try

var data = {
    "features": [{
        "type": "feature",
        "properties": {
            "id": "001",
            "name": "Example"
        }
    }]
};

alert(data.features[0].type);

Note: features array contains object, so you need to enclose that with {}

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

3 Comments

If you ever want to test, use jslint.com and paste in your json; it will validate it for you.
thanx, and what if it would be more than one object in feature , should i iterate it first to put the result into an array?
Yep. {"features":[{"id":"001"},{"id":"002"}]}

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.