Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

A json object has a key lastLogin. Value of it is a string.

I am trying to print firstName John and Blake

$scope._users = [{
        "User": {
            "userid": "dummy",
            "lastlogin": "{\"employees\":[{\"firstName\":\"John\"},   {\"firstName\":\"Blake\"}]}",
        }
    }];

https://jsfiddle.net/xu0vqmff/

Any help would be appreciated.

share|improve this question
2  
Pet peeve: There are no JSON objects in JavaScript. There are objects, and there are JSON strings. $scope._users[0] is an example of an object (not JSON object). $scope._users[0].User.lastlogin is an example of a JSON string. – Amadan Jul 9 '15 at 2:33
    
JavaScript is case-sensitive. lastlogin !== lastLogin. – tmack Jul 9 '15 at 2:42
up vote 1 down vote accepted

Try like this

View

<div ng-controller="MyCtrl">
    <div ng-repeat="user in _users" ng-init="myInfo=parJson(user.User.lastlogin)">
        <div ng-repeat="emp in myInfo.employees">{{emp.firstName}}</div>
    </div>
</div>

Controller

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.getName = function (user) {
        return "Names";
    };

    $scope._users = [{
        "User": {
            "userid": "dummy",
                "lastlogin": "{\"employees\":[{\"firstName\":\"John\"},                   {\"firstName\":\"Blake\"}]}",
        }
    }];
    $scope.parJson = function (json) {
        return JSON.parse(json);
    }
    //console.log(JSON.parse($scope._users[0].User.lastlogin));
}

JSFIDDLE

share|improve this answer
2  
Anik, why would you use JSON.parse instead of the built-in angular.fromJson? – Daniel Nalbach Jul 9 '15 at 2:56
    
@DanielNalbach , does it make any big difference between JSON.parse and angular.fromJson ? – Anik Islam Abhi Jul 9 '15 at 4:25
    
Not really, angular.fromJson is just a wrapper over JSON.parse with a string check to prevent double parsing. I thought it did more, but checked the source, and that's it. – Daniel Nalbach Jul 9 '15 at 15:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.