jQuery.getJSON() - 可以从JSON获取数据

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (195)

我有JSON数据,我想从特定的属性(recordstatus)获取数据。问题是节点路径是x.result.68089.recordstatus但是68089在jQuery调用中不起作用并且是动态的,每个记录都是不同的数字。有一种方法可以直接找到recordstatus的值吗?

JS档案:

                function getObjects(obj, key, val) {
                var objects = [];
                for (var i in obj) {
                    if (!obj.hasOwnProperty(i)) continue;
                    if (typeof obj[i] == 'object') {
                        objects = objects.concat(getObjects(obj[i], key, val));
                    } else if (i == key && obj[key] == val) {
                        objects.push(obj);
                    }
                }
                return objects;
                }

                jQuery('a[id^="link"]',window.parent.document).each(function(index) {

                    var objRECSTATUS=jQuery(this);
                    var url=decodeURIComponent(objRECSTATUS.attr('href'));
                    var l=jQuery(this).html();

                        if (l.indexOf("print")>0) {
                        var pmid= getParameterByName("pmid", url);

                        jQuery.ajax({
                                    type: "GET",
                                    url: "data.json",
                                    dataType: 'json',
                                    success: function(data) {
                                        console.log(data);

                                        if (data.result[*].recordstatus == 26) {

                                        console.log("OK 1");


                                        objRECSTATUS.removeAttr("href",json.header.version);
                                        objRECSTATUS.replaceWith(" <span><img src='http://wiki.apache.org/wiki/modernized/img/alert.png' Status 26)</span> Status 26");
                                        }
                                    else if
                                    (data.header.type == 'book')

                                    else {
                                        objRECSTATUS.hide();
                                        console.log("KO");
                                    }



                                    },
                                    error: function(xhr, status, error) {
                                        objRECSTATUS.hide();
                                    }
                                });


                        }



                    } );



                }

JSON文件

            {
                "header": {
                    "type": "cdrom",
                    "version": "6.3"
                },
                "result": {
                    "uids": [
                        "268089"
                    ],
                    "268089": {
                        "uid": "268089",
                        "pubdate": "1999",
                        "paperpubdate": "2012",
                        "source": "History",
                        "authors": [
                            {
                                "name": "John",
                                "authtype": "Author",
                                "clusterid": ""
                            }
                        ],
                        "lastauthor": "David",
                        "title": "World History.",
                        "sorttitle": "world history",
                        "volume": "2",
                        "issue": "9",
                        "pages": "365",
                        "lang": [
                            "eng"
                        ],
                        "nlmuniqueid": "89",
                        "issn": "1947-3211",
                        "essn": "14680-2199",
                        "pubtype": [
                            "Book"
                        ],
                        "status": "Online",
                        "recordstatus": "26",
                        "cover": ""
                    }
                }
            }

非常感谢帮助

提问于
用户回答回答于

看起来你的JSON文档有一个uid列表(即使只有一个列表。)

您可以使用该列表浏览记录:

success: function(data) {
   // assuming there is a uid and you are only grabbing a single record
   var uid = data.result.uids[0]

   if (data.result[uid].recordstatus == 26) {
      ...your code here
   }

扫码关注云+社区

领取腾讯云代金券