首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >jQuery.getJSON() -可以从JSON获取数据

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

Stack Overflow用户
提问于 2019-06-05 07:03:44
回答 1查看 74关注 0票数 0

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

JS文件:

代码语言:javascript
复制
                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文件

代码语言:javascript
复制
            {
                "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": ""
                    }
                }
            }

非常感谢您的帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-05 07:52:46

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

您可以使用该列表在记录之间导航:

代码语言:javascript
复制
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
   }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56452393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档