0

JSON data is in following formate. I want to access it into $http.get method. but getting something like not defined.

Angularjs

 $http.get("http://192.168.206.133:8080/admin/metering/project/")
    .success(function(response) {
      alert(JSON.stringify(response.instances));
      //$scope.groups = response.instances[0];
      //$scope.group_by = $scope.groups[0];
    });

JSON DATA

[
    {
        "instances": [
            {
                "Name": "test2",
                "Id": "7a3a2eab-7d3b-498b-bc69-ba39396ada4f"
            },
            {
                "Name": "test1",
                "Id": "1114cb76-f3df-4c60-8b12-5ad14224ffbd"
            },
            {
                "Name": "ce-2",
                "Id": "8b97b82b-a9e4-4fe0-adcb-eeaaac170301"
            },
            {
                "Name": "ce-1",
                "Id": "afaa50ad-8025-415b-81c4-566c8e06f388"
            }
        ]
    }
]

2 Answers 2

3

Try following snipet

$http.get("http://192.168.206.133:8080/admin/metering/project/")
    .success(function(response) {
      alert(JSON.stringify(response[0].instances));
      //$scope.groups = response.instances[0];
      //$scope.group_by = $scope.groups[0];
    });
Sign up to request clarification or add additional context in comments.

8 Comments

sorry nothing happing, why you are using data. This is not any where in JSON data response
This alert(JSON.stringify(response[0].instances)); is correct
He used response.data.property because the original XmlHttpRequest in JavaScript is sending the whole response object back, which contains a field data containing the requested data. The $http wraps this mechanism and strips the data from the response before it sends it back.
I used to do the same... :)
@NeelabhSingh but why using json stringify method? you can achieve it by below method
|
0

Try this in jquery format

var data = [
{
    "instances": [
        {
            "Name": "test2",
            "Id": "7a3a2eab-7d3b-498b-bc69-ba39396ada4f"
        },
        {
            "Name": "test1",
            "Id": "1114cb76-f3df-4c60-8b12-5ad14224ffbd"
        },
        {
            "Name": "ce-2",
            "Id": "8b97b82b-a9e4-4fe0-adcb-eeaaac170301"
        },
        {
            "Name": "ce-1",
            "Id": "afaa50ad-8025-415b-81c4-566c8e06f388"
        }
    ]
}]

var groups = data[0].instances;
for (i=0; i < groups.length; i++){
console.log(JSON.stringify(groups[i]));}

Check this angularjs jsfiddle for angularjs way

5 Comments

This is just a copy from the other answer? Care to add anything ?:o
nope its not copy as you can see a little difference of "[]" array braces
There was no added value in your original answer nor was it correct for that matter.
@skubski please check my fiddle and let me know
I'm not sure if that was the purpose. The original idea was to make a request to an external resource while you are using a global variable. Your fiddle works but I don't think that it is what he was trying to archieve. On a sidenote you should take a look into the controllerAs syntax AngularJS provides since 1.3.x (Not really sure) so you can get rid of $scope and use namespacing instead. @TheMechanic

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.