'Angular js Parsing Json object
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\"}]}",
}
}];
Any help would be appreciated.
Solution 1:[1]
To parse "lastlogin": "{\"employees\":[{\"firstName\":\"John\"}, {\"firstName\":\"Blake\"}]}" data correctly you can use
angular.fromJson(User.lastLogin);
Just make sure that you are giving inner object i.e. lastlogin to the method and it will remove all invalid characters such as \ and " from the json data.
Solution 2:[2]
A simple method using angular filter worked for me.
Suppose, you have a variable category which has value "{\"name\": \"Pankaj\" }". Now, I need to show the value Pankaj , for this I can use filters in this way.
<p>{{ category | nameFilter : category }}</p>
In filter filter -
var app = angular.module('userFilters', [])
.filter('nameFilter', function() {
return function(input, total) {
return JSON.parse(input).name;
};
})
Don't forget to import this file in main index.html file and pass in app.js route.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anonymous |
| Solution 2 | Pankaj Tanwar |
