如何访问每个请求的键不同的json对象值?

时间:2016-02-15 18:43:36

标签: javascript jquery json

如何每次访问密钥不同的值?

这是json对象,我需要 title 键的值。

{
    "batchcomplete": "",
    "continue": {
        "gsroffset": 10,
        "continue": "gsroffset||"
    },
    "query": {
        "pages": {
            "76241": {
                "pageid": 76241,
                "ns": 0,
                "title": "Mount Shasta",
                "index": 8,
                "contentmodel": "wikitext",
                "pagelanguage": "en",
                "pagelanguagehtmlcode": "en",
                "pagelanguagedir": "ltr",
                "touched": "2016-02-14T12:14:09Z",
                "lastrevid": 702893013,
                "length": 29944
            },
            "418135": {
                "pageid": 418135,
                "ns": 0,
                "title": "Shasta",
                "index": 1,
                "contentmodel": "wikitext",
                "pagelanguage": "en",
                "pagelanguagehtmlcode": "en",
                "pagelanguagedir": "ltr",
                "touched": "2015-12-07T21:31:59Z",
                "lastrevid": 688618455,
                "length": 2827
            }, ...

3 个答案:

答案 0 :(得分:2)

var jsonObject = {};
for(var i in jsonObject.query.pages) {

    // i will be 76241 , 418135
    console.log(jsonObject.query.pages[i].title);
}

答案 1 :(得分:1)

您可以循环data.query.pages以获取密钥及其数据。

for(var key in data.query.pages){
    if(data.query.pages.hasOwnProperty(key){
        console.log(data.query.pages[key].title);
    }
}

您也可以使用Object.keys

var keys = Object.keys(data.query.pages);
keys.forEach(function(key){
    console.log(data.query.pages[key].title);
});

答案 2 :(得分:1)

使用$.each()迭代json字符串并捕获标题:

$(document).ready(function(){

   $.each(json.query.pages, function(key, value){

     $("#res").append('<br />' + value.title);

   });

});

var json = {
    "batchcomplete": "",
    "continue": {
        "gsroffset": 10,
        "continue": "gsroffset||"
    },
    "query": {
        "pages": {
            "76241": {
                "pageid": 76241,
                "ns": 0,
                "title": "Mount Shasta",
                "index": 8,
                "contentmodel": "wikitext",
                "pagelanguage": "en",
                "pagelanguagehtmlcode": "en",
                "pagelanguagedir": "ltr",
                "touched": "2016-02-14T12:14:09Z",
                "lastrevid": 702893013,
                "length": 29944
            },
            "418135": {
                "pageid": 418135,
                "ns": 0,
                "title": "Shasta",
                "index": 1,
                "contentmodel": "wikitext",
                "pagelanguage": "en",
                "pagelanguagehtmlcode": "en",
                "pagelanguagedir": "ltr",
                "touched": "2015-12-07T21:31:59Z",
                "lastrevid": 688618455,
                "length": 2827
            }
         }
      }
    };


$(document).ready(function(){

$.each(json.query.pages, function(key, value){

  $("#res").append('<br />' + value.title);
});

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="res"></div>