使用提供的json文件中的.getJSON访问Json数据

时间:2015-09-20 20:27:03

标签: jquery json

我正在尝试从下面发布的json文件中访问“The Modern Web”一书的标题。我使用以下

$(document).ready(function() {

  $.getJSON("json/example4.json", function(data) {
    $("<div>" + data.title + "</div>").appendTo("#vtab4");

  });
});
{
  "ISBN:1593274874": {
    "publishers": [{
      "name": "No Starch Press"
    }],
    "pagination": "xiv, 243 p.",
    "table_of_contents": [{
        "title": "Introduction",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "The web platform",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "Structure and semantics",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "Device-responsive CSS",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "New approaches to CSS layouts",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "Modern JavaScript",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "Device APIs",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "Images and graphics",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "New forms",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "Multimedia",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "Web apps",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "The future",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "Appendixes.",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "Browser support as of March 2013",
        "label": "",
        "pagenum": "",
        "level": 0
      },
      {
        "title": "Further reading",
        "label": "",
        "pagenum": "",
        "level": 0
      }
    ],
    "title": "The Modern Web",
    "url": "https://openlibrary.org/books/OL25427547M/The_Modern_Web",
    "identifiers": {
      "isbn_13": [
        "9781593274870"
      ],
      "openlibrary": [
        "OL25427547M"
      ],
      "isbn_10": [
        "1593274874"
      ]
    },
    "cover": {
      "small": "https://covers.openlibrary.org/b/id/7257680-S.jpg",
      "large": "https://covers.openlibrary.org/b/id/7257680-L.jpg",
      "medium": "https://covers.openlibrary.org/b/id/7257680-M.jpg"
    },
    "publish_date": "2013",
    "key": "/books/OL25427547M",
    "authors": [{
      "url": "https://openlibrary.org/authors/OL6940720A/Peter_Gasston",
      "name": "Peter Gasston"
    }],
    "subtitle": "multi-device Web development with HTML5, CSS3, and JavaScript",
    "publish_places": [{
      "name": "San Francisco, CA"
    }]
  }
}

我无法弄清楚如何访问文档中的任何内容,即使我已经通过不同的网站验证它是无效的。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

$(document).ready(function () {
    $.getJSON("json/example4.json", function (data) {
        $.each(data, function (key, value) {
            $("<div>" + value.title + "</div>").appendTo("#vtab4");
        });
    });
});

<强>更新

Demo

答案 1 :(得分:0)

如果数据是上面的,则所有字段都在ISBN:1593274874对象内。因此,您需要使用data.["ISBN:1593274874"].title来访问标题。

JSONFormatter这样的网站允许您格式化查看哪些对象在彼此内部。