0

I'm very new to JSON, so please don't scald me. I'm trying to find a way to retrieve information from this bit of data, but I'm having zero luck.

var services = [ "Hernia Repair", "Hip Replacement" ... ];

var places = {
  "place": [
      {
        "name": "The Whittington Hospital",        
        "service": [
          "Hernia Repair",
            {
              "ratings": [
                { "name": "John Doe", "score": 4, "review": "Some text here." },
                { "name": "Jane Doe", "score": 5, "review": "Some more text." }
              ]
            }
            ...
          ]
      },
  ]
};

I'm trying to grab the name of the service and it's ratings's name and score and I've been trying to output it to the console like this:

places.place[0].service.ratings.name
places.place[0].service.ratings.score

But that doesn't seem to be working. If anyone could lend a hand to explain if my nesting is correct or if I'm retrieving them in the correct way.

Any help with this is appreciated.

P.S. Is there also a way to elegantly link the "Hernia Repair" string in the services array to the "Hernia Repair" string in the place object?

realph
  • 4,481
  • 13
  • 49
  • 104
  • Please note that your problem has nothing to do with JSON at all! You are working with JavaScript arrays and objects, not with JSON. – Felix Kling Jul 30 '14 at 22:02
  • Not sure what you mean by "link" in your PS..? – thebjorn Jul 30 '14 at 22:05
  • 1
    I am a bit annoyed this has been closed as a duplicate, because 50% of the problem here is not explained in that answer: http://jsfiddle.net/W5yY9/1/ – Fenton Jul 30 '14 at 22:11
  • @SteveFenton Thanks for this! But is there an elegant way to have more than one `service` without repeating the `service` object over and over again? – realph Jul 31 '14 at 12:02

1 Answers1

0

Service is a list, and ratings is a list, so this should do it:

places.place[0].service[1].ratings[0].name
thebjorn
  • 26,297
  • 11
  • 96
  • 138