1

I have a JSON Response as follows JSON :

var res =   
     {
      "response": {
        "data": {
          "profilesearchsnippet": [
            [
              {
                "profileInfo": {
                  "firstname": "Sundar",
                  "lastname": "v",
                  "gender": "male",
                  "country": "Afghanistan",
                  "state": "Badakhshan",
                  "city": "Eshkashem",
                  "pincode": "",
                  "plancode": "T001",
                  "userid": 13
                },
                "roleInfo": {
                  "defaultphotoid": 94
                }
              }
            ],
            [
              {
                "profileInfo": {
                  "firstname": "ghg",
                  "lastname": "vbhvh",
                  "gender": "male",
                  "state": "Badakhshan",
                  "city": "Eshkashem",
                  "pincode": "454",
                  "plancode": "T001",
                  "userid": 22
                },
                "roleInfo": {
                  "defaultphotoid": 171
                }
              }
            ]
          ]
        }
      }
    }

I want to fetch all firstname , country state city in a table.I tried assigning profilesearchsnippet value to variable var SearchData and try fetching firstname by using profileinfo,since its object. I am missing something somewhere need assistance.

HTML:

 <tr ng-repeat= "item in searchData">
        <td>{{item.profileInfo.firstname}}</td>
        <td> {{item.profileInfo.country}}</td>
      </tr>

JS:

var searchData = res.response.data.profilesearchsnippet[0];
3
  • Have you assigned searchData to something like controller or scope ? Commented Dec 26, 2016 at 5:55
  • searchData is not the iterable you are looking for, you defined it as var searchData = res.response.data.profilesearchsnippet[0]; this is just one of the search results. Try without the [0]. Commented Dec 26, 2016 at 5:57
  • embed.plnkr.co/XQismiIYFLTDy2y7sdQg Commented Dec 26, 2016 at 6:02

4 Answers 4

2

In Js get like this:-

var searchData = res.response.data.profilesearchsnippet[0];;

In Html:-

 <div ng-repeat= "item in searchData">
 <tr ng-repeat= "itm in item">
        <td>{{itm.profileInfo.firstname}}</td>
        <td> {{itm.profileInfo.country}}</td>
      </tr>
 <div>
Sign up to request clarification or add additional context in comments.

Comments

2
<tr ng-repeat= "item in searchData">
    <td>{{item[0].profileInfo.firstname}}</td>
    <td> {{item[0].profileInfo.country}}</td>
  </tr>

The data that you want to access is inside another array with single value so access it with index.

Comments

1

You will get only one using:

var searchData = res.response.data.profilesearchsnippet[0];

Try this:

var searchData = res.response.data.profilesearchsnippet;

and in template:

<tr ng-repeat= "item in searchData">
    <td>{{item[0].profileInfo.firstname}}</td>
    <td> {{item[0].profileInfo.country}}</td>
  </tr>

UPDATED:

item[0].profileInfo

Hope this will work.

Comments

1

Hope this is your requirement and you dint have country field for GHG so it wont show up in output instead that i added a static msg

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <div ng-repeat= "item in searchData">
        <p>{{item[0].profileInfo.firstname}}</p>
         <p>{{item[0].profileInfo.country ||"no country data available "}}</p>
        
      </div>
  </body>
<script>
  var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = {
  "response": {
    "data": {
      "profilesearchsnippet": [
        [
          {
            "profileInfo": {
              "firstname": "Sundar",
              "lastname": "v",
              "gender": "male",
              "country": "Afghanistan",
              "state": "Badakhshan",
              "city": "Eshkashem",
              "pincode": "",
              "plancode": "T001",
              "userid": 13
            },
            "roleInfo": {
              "defaultphotoid": 94
            }
          }
        ],
        [
          {
            "profileInfo": {
              "firstname": "ghg",
              "lastname": "vbhvh",
              "gender": "male",
              "state": "Badakhshan",
              "city": "Eshkashem",
              "pincode": "454",
              "plancode": "T001",
              "userid": 22
            },
            "roleInfo": {
              "defaultphotoid": 171
            }
          }
        ]
      ]
    }
  }
};
$scope.searchData = $scope.name.response.data.profilesearchsnippet;

});

</script>
</html>

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.