首页 > javascript > 如何通过“复杂架构”获取JSON数据

如何通过“复杂架构”获取JSON数据 (How to have a JSON data through a “complex architecture”)

2019-03-05 javascriptjson

问题

我使用的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
            };

并继续食谱我使用一个数组:

var petsRecipe = [];

for (var x = 0; x < petsTable.recipe.length; x++) {
    petsRecipe.push(petsTable.recipe[x]);
}

此数组结果: Array(8)[Object,Object,Object,Object,Object,Object,Object,Object]

第一个Object的示例是:

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/...

但我不能收集id或“lvl”,有你的想法吗?谢谢你的帮助

解决方法

recipe数组的问题是它包含的每个对象都有这样的结构:

{
    "Poudre glaciale": {
        "id": "13154",
        "lvl": "190",
        "quantity": "7"
    }
}

换句话说,每个对象都有一个键(每次都有不同的名称),并且您想要访问的对象嵌套在此变量键下,这使得访问变得棘手。

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

const 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"
    }
  }
]

const output = recipe.map( obj => {
     let keyName = Object.keys(obj)[0]; // "Galet brasillant", "poudre glaciale", etc.
     
     return obj[keyName]; // the object containing attributes
})

console.log(output[0])


编辑:

如果你想要“Poudre glaciale”的id,你必须这样做:

首先,过滤recipe数组以仅保留所需的对象:recipe.filter( obj => Object.keys(obj)[0] === "Poudre glaciale")

然后,使用此过滤后的数组中提取第一个(也是唯一的)对象 [0]

然后输入"Poudre glaciale"密钥内部["Poudre glaciale"]

然后你可以访问 id

所以完整的解决方案:

const id = recipe.filter( obj => Object.keys(obj)[0] === "Poudre glaciale")[0]["Poudre glaciale"].id

问题

I work with an API that returns a rather particular JSON file ... It's been 2 days that I try to do something but I can not do it, that's why I leave it to you :)

First of all my JSON file looks like:

[ 
    {
        "_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 the moment i extract data like that:

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
            };

and to go on recipe i use an array :

var petsRecipe = [];

for (var x = 0; x < petsTable.recipe.length; x++) {
    petsRecipe.push(petsTable.recipe[x]);
}

This array result : Array(8) [Object, Object, Object, Object, Object, Object, Object, Object]

And an example of the first Object is :

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/...

But I can not collect the id or the "lvl", have you ideas ? Thanks for your help

解决方法

The problem with the recipe array is that every object it contains has a structure like this :

{
    "Poudre glaciale": {
        "id": "13154",
        "lvl": "190",
        "quantity": "7"
    }
}

In other words, each object has one key (with a different name each time), and the object you want to reach is nested under this variable key, which makes it tricky to reach.

You can access the key name ("poudre glaciale") using Object.keys(obj)[0]. Then you can reach the sub-object easily :

const 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"
    }
  }
]

const output = recipe.map( obj => {
     let keyName = Object.keys(obj)[0]; // "Galet brasillant", "poudre glaciale", etc.
     
     return obj[keyName]; // the object containing attributes
})

console.log(output[0])

Edit :

If you want the id of "Poudre glaciale", you have to do this :

First, filter the recipe array to keep only the object you want : recipe.filter( obj => Object.keys(obj)[0] === "Poudre glaciale")

Then, extract the first (and only) object from this filtered array with [0]

Then enter inside the "Poudre glaciale" key with ["Poudre glaciale"]

And then you can access the id

So the full solution :

const id = recipe.filter( obj => Object.keys(obj)[0] === "Poudre glaciale")[0]["Poudre glaciale"].id
相似信息