如何访问JavaScript内的json对象内的嵌套值 [英] how to access nested values inside json object inside javascript

查看:121
本文介绍了如何访问JavaScript内的json对象内的嵌套值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



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



我附上我的代码我已经写了:

$(document).ready(function(){
变种globalJsonVar;
警报( 检查);
$ .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); //我尝试访问值
} else {
alert( 骗局);
}
});
});

我收到的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,
系列:[
企鹅经典
$ blc_classifications:[
PR9499.3.N3 G85 2006

latest_revision:7,
uri_descriptions: [
贡献者履历信息,
发布者描述

流派:[
虚构。

title:The guide,
languages:[{
key:/ languages / eng
}]],
科目:[
Malgudi(India:Imaginary place) - Fiction。,
导游(人物) - 小说。,
Bharata natyam舞蹈家 - 小说,
精神生活 - 印度教 - 小说,
印度 - 虚构。
],
publish_country:nyu,
by_statement:RK Narayan; 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:[
纽约
],
oclc_number:[
65644730
],
分页: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

注释:{
type:/ type / text,
价值:最初出版:纽约:维京出版社,1958年。包括书目参考资料(第[xxi] -xxii)。
},
标识符:{
goodreads:[
129877
],
librarything:[
6930
]
},
isbn_13:[
9780143039648
],
lccn:[
2006044314
],
isbn_10 :[
0143039644
],
publish_date:2006,
works:[{
key:/ works / OL105 7183W
]
}
}
}




解决方案

  result.ISBN:9781606868829.bib_key 

是语法错误。 在这里是不允许的。您想使用以下语法:

  result [ISBN:9781606868829]。bib_key 


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"
      }]
    }
  }
}

解决方案

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

这篇关于如何访问JavaScript内的json对象内的嵌套值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆