Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用未知键遍历Javascript中的JSON对象_Javascript_Object - Fatal编程技术网

使用未知键遍历Javascript中的JSON对象

使用未知键遍历Javascript中的JSON对象,javascript,object,Javascript,Object,我从API中得到一个JSON字符串,如下所示: { "batchcomplete": "", "query": { "pages": { "682482": { "pageid": 682482, "ns": 0,

我从API中得到一个JSON字符串,如下所示:

{
    "batchcomplete": "",
    "query": {
        "pages": {
            "682482": {
                "pageid": 682482,
                "ns": 0,
                "title": "Human",
                "thumbnail": {
                    "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Akha_cropped_hires.JPG/119px-Akha_cropped_hires.JPG",
                    "width": 119,
                    "height": 200
                },
                "extract": "Humans (Homo sapiens) are a species of highly intelligent primates. They are the only extant members of the subtribe Hominina and—together with chimpanzees, gorillas, and orangutans—are part of the family Hominidae (the great apes, or hominids). Humans are terrestrial animals, characterized by their erect posture and bipedal locomotion; high manual..."
            }
        }
    }
}
目标
我永远不知道页面ID是什么,我需要检索提取缩略图。 提取应该是字符串,缩略图应该是对象

如果没有随机的pageid,问题就不大了

我尝试过的

const thumbnail = jsonObj.query[0].thumbnail;
const extract = jsonObj.query[0].extract;
预期结果

thumbnail = {
              "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Akha_cropped_hires.JPG/119px-Akha_cropped_hires.JPG",
              "width": 119,
              "height": 200
            }

extract = "Humans (Homo sapiens) are a species of highly intelligent primates. They are the only extant members of the subtribe Hominina and—together with chimpanzees, gorillas, and orangutans—are part of the family Hominidae (the great apes, or hominids). Humans are terrestrial animals, characterized by their erect posture and bipedal locomotion; high manual...";

这有点像黑客,但是如果你知道永远只有一个pageid(或者如果你总是想要第一个),你可以使用:

函数返回一个数组,其中包含给定对象中的所有键。然后,您可以使用返回的数组使用对象的任何键来访问某些值

所以你可以这样做:

{
    "batchcomplete": "",
    "query": {
        "pages": {
            "682482": {
                "pageid": 682482,
                "ns": 0,
                "title": "Human",
                "thumbnail": {
                    "source": "https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Akha_cropped_hires.JPG/119px-Akha_cropped_hires.JPG",
                    "width": 119,
                    "height": 200
                },
                "extract": "Humans (Homo sapiens) are a species of highly intelligent primates. They are the only extant members of the subtribe Hominina and—together with chimpanzees, gorillas, and orangutans—are part of the family Hominidae (the great apes, or hominids). Humans are terrestrial animals, characterized by their erect posture and bipedal locomotion; high manual..."
            }
        }
    }
}
const partialObj={
“682482”:“值”
};
const keys=Object.keys(partialObj);
控制台日志(键);
//然后,您可以使用“未知”键访问该值:
console.log(partialObj[key[0]]);//=>值
例如和的组合将完成此工作

const示例={
“批处理完成”:“,
“查询”:{
“页数”:{
"682482": {
“pageid”:682482,
“ns”:0,
“头衔”:“人类”,
“缩略图”:{
“来源”:https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/Akha_cropped_hires.JPG/119px-Akha_cropped_hires.JPG",
“宽度”:119,
“高度”:200
}
}
}
}
}
常数{
pageid,
缩略图,
}=Object.values(sample.query.pages)[0];
console.log(pageid);
控制台日志(缩略图)

.as控制台包装{min height:100%!important;top:0;}
这里是一个使用

优点是更易于维护(例如,如果响应结构发生变化,则针对不同的键)

//const objectScan=require('object-scan');
const myData={batchcomplete:'',查询:{pages:{682482:{pageid:682482,ns:0,标题:'Human',缩略图:{source:'…url…',宽度:119,高度:200},摘录:'…text…'}};
const extract=(data)=>objectScan(['query.pages....{缩略图,extract}']{
filterFn:({property,value,context})=>{
上下文[属性]=值;
}
})(数据,{});
console.log(提取(myData));
/* => {
摘录:“…文本…”,
缩略图:{source:'…url…',宽度:119,高度:200}
}*/
。作为控制台包装{最大高度:100%!重要;顶部:0}

不错,甚至都不会称之为黑客。只是一个简单的解决方案,漂亮地解决了问题。谢谢分享!