1

I have a JsonString of the form

[{
    "mileage": 12033,
    "name": "Ford",
    "model": "Focus",
    "engine": "3 gophers on a treadmill",
    "color": "green"
}, {
    "mileage": 85000,
    "name": "Chevy",
    "model": "mailbu",
    "engine": "6 cylinder",
    "color": "Maroon"
}, {
    "mileage": 612033,
    "name": "Ford",
    "model": "F150",
    "engine": "6 cylinder",
    "color": "Green"
}, {
    "mileage": 89500,
    "name": "Pontiac",
    "model": "G6",
    "engine": "2 cylinder",
    "color": "Blue"
}, {
    "mileage": 17200,
    "name": "Pontiac",
    "model": "G8",
    "engine": "3 cylinder",
    "color": "Silver"
}, {
    "mileage": 308,
    "name": "Toyota",
    "model": "Forerunner",
    "engine": "6 cylinders",
    "color": "Grey"
}, {
    "mileage": 65328,
    "name": "Volvo",
    "model": "wagon",
    "engine": "8 cyclinders while not in test mode otherwise 2 bicycle pedals",
    "color": "Red"
}, {
    "mileage": 50,
    "name": "IKea",
    "model": "Ronde",
    "engine": "3 self propelled coasters",
    "color": "wood panelling"
}, {
    "mileage": 17200,
    "name": "Pontiac",
    "model": "Grand Am",
    "engine": "8 cylinder",
    "color": "rusty"
}]

Can someone please help me convert this string into an array in angular.js. I tried to do it for hours and have to finally giveup. Thanks in advance

5
  • 4
    Possible duplicate of How to decode JSON in Javascript? Commented Feb 24, 2017 at 11:38
  • 1
    you could use JSON.parse to get an array of the string. Commented Feb 24, 2017 at 11:38
  • 4
    Its already an array Commented Feb 24, 2017 at 11:38
  • This is array of objects already! what you want? Commented Feb 24, 2017 at 11:39
  • I think it is alreay array of objects Commented Feb 24, 2017 at 11:40

1 Answer 1

0

You have to assign it inside the app controller and then you can access it like an array in your view.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl"> 

<p>Today's welcome message is:</p>

<p ng-repeat="x in car">{{x.name}}</p>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
  $scope.car = [{
    "mileage": 12033,
    "name": "Ford",
    "model": "Focus",
    "engine": "3 gophers on a treadmill",
    "color": "green"
}, {
    "mileage": 85000,
    "name": "Chevy",
    "model": "mailbu",
    "engine": "6 cylinder",
    "color": "Maroon"
}, {
    "mileage": 612033,
    "name": "Ford",
    "model": "F150",
    "engine": "6 cylinder",
    "color": "Green"
}, {
    "mileage": 89500,
    "name": "Pontiac",
    "model": "G6",
    "engine": "2 cylinder",
    "color": "Blue"
}, {
    "mileage": 17200,
    "name": "Pontiac",
    "model": "G8",
    "engine": "3 cylinder",
    "color": "Silver"
}, {
    "mileage": 308,
    "name": "Toyota",
    "model": "Forerunner",
    "engine": "6 cylinders",
    "color": "Grey"
}, {
    "mileage": 65328,
    "name": "Volvo",
    "model": "wagon",
    "engine": "8 cyclinders while not in test mode otherwise 2 bicycle pedals",
    "color": "Red"
}, {
    "mileage": 50,
    "name": "IKea",
    "model": "Ronde",
    "engine": "3 self propelled coasters",
    "color": "wood panelling"
}, {
    "mileage": 17200,
    "name": "Pontiac",
    "model": "Grand Am",
    "engine": "8 cylinder",
    "color": "rusty"
}];

});
</script>

</body>
</html>
Sign up to request clarification or add additional context in comments.

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.