Javascript 如何通过";“复杂建筑”;

Javascript 如何通过";“复杂建筑”;,javascript,json,Javascript,Json,我使用的API返回一个非常特殊的JSON文件。。。 已经两天了,我试着做一些事情,但我做不到,这就是为什么我把它留给你:) 首先,我的JSON文件如下所示: [ { "_id": 14080, "name": "Amulette Séculaire", "lvl": "200", "type": "Amulette", "imgUrl": "https://s.ankama.com/www/static.a

我使用的API返回一个非常特殊的JSON文件。。。 已经两天了,我试着做一些事情,但我做不到,这就是为什么我把它留给你:)

首先,我的JSON文件如下所示:

[ 
    {
        "_id": 14080,
        "name": "Amulette Séculaire",
        "lvl": "200",
        "type": "Amulette",
        "imgUrl": "https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/200/1230.png",
        "url": "https://www.dofus.com/fr/mmorpg/encyclopedie/equipements/14080-amulette-seculaire",
        "description": "Finalement, le secteur de la bijouterie n'a pas tellement évolué ces cent dernières années.",
        "stats": [
            {
                "Vitalité": {
                    "from": "251",
                    "to": "300"
                }
            },
            {
                "Intelligence": {
                    "from": "61",
                    "to": "80"
                }
            },
            {
                "Agilité": {
                    "from": "16",
                    "to": "25"
                }
            },
            {
                "Sagesse": {
                    "from": "31",
                    "to": "40"
                }
            },
            {
                "PA": {
                    "from": "1"
                }
            },
            {
                "Prospection": {
                    "from": "16",
                    "to": "20"
                }
            },
            {
                "Dommages Feu": {
                    "from": "8",
                    "to": "12"
                }
            },
            {
                "Dommages Air": {
                    "from": "8",
                    "to": "12"
                }
            },
            {
                "% Résistance Neutre": {
                    "from": "6",
                    "to": "8"
                }
            },
            {
                "% Résistance Feu": {
                    "from": "6",
                    "to": "8"
                }
            },
            {
                "Résistance Critiques": {
                    "from": "11",
                    "to": "15"
                }
            }
        ],
        "condition": [],
        "recipe": [
            {
                "Galet brasillant": {
                    "id": "12740",
                    "url": "https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/12740-galet-brasillant",
                    "imgUrl": "https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/15289.w48h48.png",
                    "type": "Galet",
                    "lvl": "150",
                    "quantity": "3"
                }
            },
            {
                "Poudre glaciale": {
                    "id": "13154",
                    "url": "https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/13154-poudre-glaciale",
                    "imgUrl": "https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/48292.w48h48.png",
                    "type": "Poudre",
                    "lvl": "190",
                    "quantity": "7"
                }
            },
            {
                "Poil d'aisselle de Missiz Frizz": {
                    "id": "13935",
                    "url": "https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/13935-poil-aisselle-missiz-frizz",
                    "imgUrl": "https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/54691.w48h48.png",
                    "type": "Poil",
                    "lvl": "200",
                    "quantity": "9"
                }
            },
            {
                "Pédoncule de Mérulor": {
                    "id": "13978",
                    "url": "https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/13978-pedoncule-merulor",
                    "imgUrl": "https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/119008.w48h48.png",
                    "type": "Champignon",
                    "lvl": "200",
                    "quantity": "6"
                }
            },
            {
                "Œil de Cycloïde": {
                    "id": "13988",
                    "url": "https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/13988-oeil-cycloide",
                    "imgUrl": "https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/109603.w48h48.png",
                    "type": "Œil",
                    "lvl": "200",
                    "quantity": "30"
                }
            },
            {
                "Queue de Sinistrofu": {
                    "id": "13991",
                    "url": "https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/13991-queue-sinistrofu",
                    "imgUrl": "https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/65764.w48h48.png",
                    "type": "Queue",
                    "lvl": "200",
                    "quantity": "3"
                }
            },
            {
                "Bandelette du Comte Harebourg": {
                    "id": "13995",
                    "url": "https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/13995-bandelette-comte-harebourg",
                    "imgUrl": "https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/15831.w48h48.png",
                    "type": "Ressources diverses",
                    "lvl": "200",
                    "quantity": "1"
                }
            },
            {
                "Œil de verre": {
                    "id": "14145",
                    "url": "https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/14145-oeil-verre",
                    "imgUrl": "https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/109604.w48h48.png",
                    "type": "Œil",
                    "lvl": "200",
                    "quantity": "1"
                }
            }
        ],
        "setId": 271
    }
]
目前,我提取的数据如下:

for (var i = 0; i < pets.length; i++) {
            petsTable = {
                name: pets[i].name,
                level: pets[i].lvl,
                type: pets[i].type,
                description: pets[i].description,
                imgUrl: pets[i].imgUrl,
                url: pets[i].url,
                condition: pets[i].condition,
                recipe: pets[i].recipe
            };
但我无法收集身份证或“lvl”,你有什么想法吗?
感谢您的帮助

配方数组的问题在于它包含的每个对象都有如下结构:

{
    "Poudre glaciale": {
        "id": "13154",
        "lvl": "190",
        "quantity": "7"
    }
}
换句话说,每个对象都有一个键(每次使用不同的名称),并且要访问的对象嵌套在此变量键下,这使得访问变得很困难

您可以使用
Object.keys(obj)[0]
访问密钥名称(“poudre Glacale”)。然后,您可以轻松地到达子对象:

const配方=[{
“Galet Brasilant”:{
“id”:“12740”,
“url”:”https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/12740-galet-brasillant",
“imgUrl”:https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/15289.w48h48.png",
“类型”:“Galet”,
“lvl”:“150”,
“数量”:“3”
}
},
{
“波德雷冰川”:{
“id”:“13154”,
“url”:”https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/13154-poudre-glaciale",
“imgUrl”:https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/48292.w48h48.png",
“类型”:“Poudre”,
“等级”:“190”,
“数量”:“7”
}
},
{
“波伊尔德艾塞尔德米西兹卷发”:{
“id”:“13935”,
“url”:”https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/13935-poil-aisselle-missiz-frizz",
“imgUrl”:https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/54691.w48h48.png",
“类型”:“Poil”,
“等级”:“200”,
“数量”:“9”
}
},
{
“梅鲁勒先生”:{
“id”:“13978”,
“url”:”https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/13978-pedoncule-merulor",
“imgUrl”:https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/119008.w48h48.png",
“类型”:“Champignon”,
“等级”:“200”,
“数量”:“6”
}
},
{
“骑自行车的人”:{
“id”:“13988”,
“url”:”https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/13988-oeil-cycloide",
“imgUrl”:https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/109603.w48h48.png",
“类型”:“Œil”,
“等级”:“200”,
“数量”:“30”
}
},
{
“中国队”:{
“id”:“13991”,
“url”:”https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/13991-queue-sinistrofu",
“imgUrl”:https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/65764.w48h48.png",
“类型”:“队列”,
“等级”:“200”,
“数量”:“3”
}
},
{
“哈雷堡伯爵乐队”:{
“id”:“13995”,
“url”:”https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/13995-bandelette-comte-harebourg",
“imgUrl”:https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/15831.w48h48.png",
“类型”:“资源差异”,
“等级”:“200”,
“数量”:“1”
}
},
{
“il de verre”:{
“id”:“14145”,
“url”:”https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/14145-oeil-verre",
“imgUrl”:https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/109604.w48h48.png",
“类型”:“Œil”,
“等级”:“200”,
“数量”:“1”
}
}
]
常量输出=recipe.map(obj=>{
让keyName=Object.keys(obj)[0];/“Galet brasillant”、“poudre Glacale”等。
返回obj[keyName];//包含属性的对象
})

console.log(输出[0])
您希望从此json访问哪些数据?您尝试过什么代码?“这就是为什么我把它留给您:)”-我们很乐意帮助您调试您的尝试,但是StackOverflow的目的不是让其他人在您放弃后编写您的代码。考虑编辑你的问题,包括一些尝试/代码。“我做错了什么?”我们怎么知道?你什么都没给我们看<代码>响应[0]。配方是包含对象的数组。每一个都有一个单独的键,你可以通过
Object.keys(obj)[0]
。我已经编辑了我的帖子:)例如,获取“poudre glaciale”id的最佳方法是什么?谢谢,要动态地实现这一点,应该用键名替换“poudre glaciale”?
0:
Galet brasillant:
id: "12740"
imgUrl: "https://s.ankama.com/www/static.ankama.com/dofus/www/game/items/52/15289.w48h48.png"
lvl: "150"
quantity: "3"
type: "Galet"
url: "https://www.dofus-touch.com/fr/mmorpg/encyclopedie/ressources/...
{
    "Poudre glaciale": {
        "id": "13154",
        "lvl": "190",
        "quantity": "7"
    }
}