how to access nested values inside json object ins

2019-08-16 23:18发布

This question already has an answer here:

I have this URL which returns me a JSON value, my task is to access 3 values out of it that is name, title and ISBN.

Since the content is heavily nested, I am not able to access the values inside it.

All the tutorials I have referred so far, have simple nesting in them

I have attached my code that I have written so far:

$(document).ready(function() {
  var globalJsonVar;
  alert("check");
  $.getJSON("https://openlibrary.org/api/books?bibkeys=ISBN:9781606868829&jscmd=details&format=json", function(result) {
    if (Object.keys(result).length > 0) {
      alert("conetnt is present");
      console.log(result);
      alert(Object.values(result));
      alert(result.ISBN: 9781606868829. bib_key); // my attempt at accessing the values
    } else {
      alert("hoax");
    }
  });
});

The JSON content that I am receiving is

{
  "ISBN:9780143039648": {
    "bib_key": "ISBN:9780143039648",
    "preview": "noview",
    "preview_url": "https://openlibrary.org/books/OL17924003M/The_guide",
    "info_url": "https://openlibrary.org/books/OL17924003M/The_guide",
    "details": {
      "number_of_pages": 196,
      "series": [
        "Penguin classics"
      ],
      "lc_classifications": [
        "PR9499.3.N3 G85 2006"
      ],
      "latest_revision": 7,
      "uri_descriptions": [
        "Contributor biographical information",
        "Publisher description"
      ],
      "genres": [
        "Fiction."
      ],
      "title": "The guide",
      "languages": [{
        "key": "/languages/eng"
      }],
      "subjects": [
        "Malgudi (India : Imaginary place) -- Fiction.",
        "Tour guides (Persons) -- Fiction.",
        "Bharata natyam dancers -- Fiction.",
        "Spiritual life -- Hinduism -- Fiction.",
        "India -- Fiction."
      ],
      "publish_country": "nyu",
      "by_statement": "R.K. Narayan ; introduction by Michael Gorra.",
      "type": {
        "key": "/type/edition"
      },
      "uris": [
        "http://www.loc.gov/catdir/enhancements/fy0716/2006044314-b.html",
        "http://www.loc.gov/catdir/enhancements/fy0716/2006044314-d.html"
      ],
      "revision": 7,
      "publishers": [
        "Penguin Books"
      ],
      "dewey_decimal_class": [
        "823/.914"
      ],
      "last_modified": {
        "type": "/type/datetime",
        "value": "2012-08-05T21:10:58.887826"
      },
      "key": "/books/OL17924003M",
      "authors": [{
        "name": "Rasipuram Krishnaswamy Narayan",
        "key": "/authors/OL5911201A"
      }],
      "publish_places": [
        "New York"
      ],
      "oclc_number": [
        "65644730"
      ],
      "pagination": "xxiv, 196 p. ;",
      "created": {
        "type": "/type/datetime",
        "value": "2008-10-07T19:30:19.584308"
      },
      "url": [
        "http://www.loc.gov/catdir/enhancements/fy0716/2006044314-b.html",
        "http://www.loc.gov/catdir/enhancements/fy0716/2006044314-d.html"
      ],
      "notes": {
        "type": "/type/text",
        "value": "Originally published: New York : Viking Press, 1958.\n\nIncludes bibliographical references (p. [xxi]-xxii)."
      },
      "identifiers": {
        "goodreads": [
          "129877"
        ],
        "librarything": [
          "6930"
        ]
      },
      "isbn_13": [
        "9780143039648"
      ],
      "lccn": [
        "2006044314"
      ],
      "isbn_10": [
        "0143039644"
      ],
      "publish_date": "2006",
      "works": [{
        "key": "/works/OL1057183W"
      }]
    }
  }
}

2条回答
劳资没心,怎么记你
2楼-- · 2019-08-16 23:54

You have to use [] if you want to have access to keys that have special chars, for example result['something-with:special-chars']

查看更多
何必那么认真
3楼-- · 2019-08-16 23:55
result.ISBN:9781606868829.bib_key

is a syntax error. : is not allowed here. You want to use the following syntay :

result["ISBN:9781606868829"].bib_key
查看更多
登录 后发表回答