0

My json file is of the format:

{
  "doc": {
    "BandDatabase": {
      "Artist1": {
        "-name": "john",
        "-instrument": "piano"
        "Artist2": [
          {
            "-name": "tina",
            "-instrument": "drums"
            "Manager": {
              "Person1": {
                "-name": "abby"
                }

I have my controller setup as :

myApp.controller('MyController', ['$scope', '$http', function ($scope, $http) {

    $http.get('js/artists.json').success(function(data) {
        $scope.artists = data;
    });

How do I search for an element in the json format above? I am unable to display the name element on the webpage my doing the following:

<div ng-controller = "MyController">
    <ul class="artists">
        <li class="messages" ng-repeat="item in artists">
            <div class="info">
                <h1>{{item.name}}</h1>
            </div> 

I want to be able to display the name of Artist1 and Artist 2.

1
  • In your JSON file, is Artist2 a property of Artist1? Does Artist1 an object and Artist2 an array? Commented Jun 21, 2014 at 22:14

1 Answer 1

1

JSON.Parse is part of JavaScript use it to convert JSON into JavaScript Objects.

$http.get('js/artists.json').success(function(data) {
    $scope.artists = JSON.Parse(data).artists;
});

BTW your JSON looks rather strange to me... I imagine it should look more like this:

{
    "artists": [
        {
            "name": "kongor",
            "instrument": "gitar"
        },
        {
            "name": "Jacob",
            "instrument:" "...",
            "manager": {
                "name": "Cole"
            }
        }
    ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that there is also an angular.fromJson function, but it's almost exactly the same thing: return isString(json) ? JSON.parse(json) : json;

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.