简体   繁体   English

如何使用JavaScript访问json中的嵌套键

[英]How to access a nested key in json using javascript

So, i have this json file, in which i have to take out the fileName tag, and use it. 所以,我有这个json文件,在其中我必须取出fileName标记并使用它。

{
"dataset": {
    "private": false,
    "stdyDscr": {
        "citation": {
            "titlStmt": {
                "titl": "Smoke test",
                "IDNo": {
                    "text": "10.5072/FK2/WNCZ16",
                    ".attrs": {
                        "agency": "doi"
                    }
                }
            },
            "rspStmt": {
                "AuthEnty": "Dataverse, Admin"
            },
            "biblCit": "Dataverse, Admin, 2015, \"Smoke test\", http://dx.doi.org/10.5072/FK2/WNCZ16,  Root Dataverse,  V1 [UNF:6:iuFERYJSwTaovVDvwBwsxQ==]"
        }
    },
    "fileDscr": {
        "fileTxt": {
            "fileName": "fearonLaitinData.tab",
            "dimensns": {
                "caseQnty": "6610",
                "varQnty": "69"
            },
            "fileType": "text/tab-separated-values"
        },
        "notes": {
            "text": "UNF:6:K5wLrMhjKoNX7znhVpU8lg==",
            ".attrs": {
                "level": "file",
                "type": "VDC:UNF",
                "subject": "Universal Numeric Fingerprint"
            }
        },
        ".attrs": {
            "ID": "f6"
        }
    }
},

im using d3.js mostly, but some parts of jquery and javascript with it. 我主要使用d3.js,但jquery和javascript的某些部分使用了它。 right now im doing: 现在我正在做:

d3.json(url,function(json){ 

              var jsondata=json;

                   var temp = jsondata.dataset.fileDscr.fileTxt.fileName;
}

Is there a way to just access fileName directly? 有没有办法直接访问fileName? Im asking because, i have to make this generic to fit other json files, where the nesting might be different. 我问是因为,我必须使这个泛型适合其他json文件,其中嵌套可能不同。

This will return the value for some instance of the key in the JSON data, if it exists. 这将返回JSON数据中某个键实例的值(如果存在)。

var data = {...};
function findValue(json, key) {
  if (key in json) return json[key];
  else {
    var otherValue;
    for (var otherKey in json) {
      if (json[otherKey] && json[otherKey].constructor === Object) {
        otherValue = findValue(json[otherKey], key);
        if (otherValue !== undefined) return otherValue;
      }
    }
  }
}
console.log(findValue(data, 'fileName'));

It will return a comma separated string of all the values of a specified key 它将返回一个逗号分隔的字符串,其中包含指定键的所有值

function walk(obj,keyname) {    
    var propertyVal="";                                                                                 
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                var val = obj[key];                                                                                     
                if(typeof(val) == 'object') {
                    console.log(val);                                                                                               
                    propertyVal+= walk(val,keyname);                                                
                }else {
                    if(key == keyname){                                                     
                        propertyVal = propertyVal+","+obj[key];                                                                     
                    }   
                }
            }
        }   
        return propertyVal;                         
}
alert(walk(data,'filename').replace(',',''));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM