如何在javascript中访问json对象内的嵌套值

时间:2017-10-31 13:39:19

标签: javascript jquery html json url

我有这个URL,它返回一个JSON值,我的任务是访问名称,标题和ISBN的3个值。

由于内容严重嵌套,我无法访问其中的值。

到目前为止我提到的所有教程都有简单的嵌套

我附上了我迄今为止编写的代码:

$(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");
    }
  });
});

我收到的JSON内容是

{
  "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 个答案:

答案 0 :(得分:-1)

如果您想要访问具有特殊字符的键,则必须使用[],例如result['something-with:special-chars']

答案 1 :(得分:-1)

result.ISBN:9781606868829.bib_key

是语法错误。此处不允许:。您想使用以下合成语:

result["ISBN:9781606868829"].bib_key