0

How can I get value of "ddd" using jquery? Here is my JSON:

{
   "aaa": "aaa",
   "bbb": {
      "ccc": {
         "ddd": "HERE IS THE DATA"
         }
   }
}

I'm quite new to JS, so pardon me for silly question.

Alexandr
  • 155
  • 2
  • 3
  • 13

1 Answers1

0

Assuming your object is named obj, you can access the value with :

obj.bbb.ccc.ddd

But this has nothing to do with jQuery. This is pure javascript.

See fiddle.

Michel
  • 26,600
  • 6
  • 64
  • 69
  • Thank you for reply! It works on jsfiddle but returns error with my file. Here is the original JSON I'm working with - [pastebin](http://pastebin.com/UBdTGuVQ). I try to get _audio_url_ with `alert(jsonStr.file_versions.mobile.audio_url);` where jsonStr is my object. And I get `Uncaught TypeError: Cannot read property 'mobile' of undefined` – Alexandr Mar 08 '15 at 01:22
  • There are two methods to access a property. The dot notation, and the bracket notation. For properties with 'special' characters, you should go with brackets. Try `jsonStr['file_versions'].mobile['audio_url']` – Michel Mar 08 '15 at 01:27
  • In the above code line, because there is no special character in the property `mobile`, I used the dot notation, but we could also write : `jsonStr['file_versions']['mobile']['audio_url']` – Michel Mar 08 '15 at 01:29
  • I've tried both variants and still get the `Uncaught TypeError: Cannot read property 'mobile' of undefined` error. If I try any high values (like 'id') I get now `undefined` instead of data. Is there something wrong with my json maybe? – Alexandr Mar 08 '15 at 01:43
  • Your json seems ok, I tried both variants and I get an url. See [updated fiddle](http://jsfiddle.net/03Lw7p5n/2/). It alerts the audio url twice, first time with the first variant, and second time with the second. – Michel Mar 08 '15 at 01:46
  • Well, now I'm completely lost. I don't get why it doesn't work for me. Here is the whole file, which returns error with `mobile` - [pastebin](http://pastebin.com/DnYQNZLu) – Alexandr Mar 08 '15 at 01:53
  • In your code `jsonStr` is a `string`, it's not an `object`. This string does not even represent a json object, but html instead. – Michel Mar 08 '15 at 02:12