1

I want to access JSON object member using angularjs. I can print the whole json object array but can't access the member of the object uniquely.

          $scope.rows = []; // init empty array
          $scope.datainput =[];

          $http({
           method: 'GET',
           url: 'Data/input.json'
        }).then(function (data){
           $scope.datainput=data;
           //console.log($scope.datainput);
           console.log($scope.datainput);


        },function (error){
           console.log("big error");
        });



        var json=JSON.parse($scope.datainput);
        console.log(json[0].status);

I have tried this code also but still geting the same error .

           $scope.temp = "";
           $scope.rows = []; // init empty array
           $scope.datainput =[];


        $http({
            method: 'GET',
            url: 'Data/input.json'
         }).then(function (data){
            $scope.datainput=data;
            //console.log($scope.datainput);
            console.log($scope.datainput);

          var json=JSON.parse($scope.datainput);
          console.log(json[0].status);

         },function (error){
            console.log("big error");
         });

json file input.json:

[
    {"status":"payfail","value":"310"},
    {"status":"payinit","value":"100"},
    {"status":"paysuccess","value":"200"},
    {"status":"payreturn","value":"50"}

]

I get this error :

SyntaxError: Unexpected end of JSON input at JSON.parse ()

The solution will be this....

           $scope.rows = []; // init empty array
           $scope.datainput =[];


        $http({
            method: 'GET',
            url: 'Data/input.json'
         }).then(function (data){
            $scope.datainput=data.data;
            //console.log(data);



            console.log($scope.datainput);

            var json=JSON.parse(JSON.stringify($scope.datainput));
            console.log(json[0].status);


         },function (error){
            console.log("big error");
         });

2 Answers 2

2

My guess would be that you are trying to parse the JSON string before it has been returned by the promise.

Try putting your parsing code inside the then block so that it executes in the correct order.

Upon further investigation, here is an updated Answer:

In addition to fixing the promise execution order, it turns out there is an issue with the way that you are accessing the data on the response variable.

Check the documentation for $http here: W3Schools.com $http doco

You will see that the callback response value actually contains a member called data for the response payload. To get this working try accessing the data member on the response object.

$scope.datainput=data.data;

It would probably be a good idea to also rename the data response object from data to response for readability.

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

12 Comments

Have you tried dumping the data object to the console to see if you are getting anything back from the HTTP request?
It worked as a whole object .But It doesn't work If I want to access member uniquely.
It sounds to me like the response may not be valid JSON? If you are getting undefined from the Parse call that would typically mean that it is unable to parse the input string.
Than what can I do about it to access all the member uniquely?
Well the first step is to ensure that the response from your service is JSON. I expect if you were to hardcode the JSON into the json object the parse option would work fine.
|
0

You can use this code to solve that:

$scope.rows = []; // init empty array
  $scope.datainput =[];

          $http({
           method: 'GET',
           url: 'Data/input.json'
        }).then(function (data){
           $scope.datainput=data.data;
           console.log($scope.datainput);

        var json= angular.fromJson($scope.datainput); 
         console.log(json[0].status);

        },function (error){
           console.log("big error");
        });

You should'n write Json.Parse out function Get you should write this code inside the then block and use data.data to get the data from json file.

7 Comments

Still getting the same error.I have tried that before.Now I have tried that again.
"angular.min.js:127 TypeError: Cannot read property 'status' of undefined" now I am getting this error
Thank for your effort @Feras but its still giving the same error.
can you give me feedback what var json retrieve data
Json var could not retrieve any data.It simply giving error. But "console.log($scope.datainput);" this command is printing the whole object.
|

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.