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

时间:2019-06-04 23:03:44

标签: jquery json ajax

我有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": ""
                    }
                }
            }

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

看起来您JSON文档中有一个uid列表(即使只有一个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
   }