简体   繁体   English

如何动态获取嵌套在 JSON 文件中的数据

[英]How to dynamically fetch data nested in JSON file

I am building a feature that can allow users to type a word, select the language the word is in and then get the word's definition and an example using this API service .我正在构建一个功能,可以让用户输入一个单词 select 单词所在的语言,然后获取单词的定义和使用此API 服务的示例。

The data I want to fetch is whatever it is at position 0 of "exclamation" of "meaning".我要获取的数据是“意义”的“感叹号”的 position 0 处的任何数据。 This way, I can make sure users get the definition and an example for the word they are looking for.这样,我可以确保用户获得他们正在寻找的单词的定义和示例。 My question is, the position of data of "definition" and "example" varies with different languages.我的问题是,“定义”和“示例”数据的 position 因语言不同而异。 As you can see, the one below is the English word's JSON data format.可以看到,下面是英文单词的JSON数据格式。

[{
    "word": "hello",
    "phonetic": [
        "həˈləʊ",
        "hɛˈləʊ"
    ],
    "meaning": {
        "exclamation": [
            {
                "definition": "used as a greeting or to begin a telephone conversation.",
                "example": "hello there, Katie!"
            }
        ]
    }
}]

And this one below is the French word's JSON data format.而下面这个是法语单词的JSON数据格式。

[
    {
        "word": "bonjour",
        "phonetic": "",
        "origin": "",
        "meaning": {
            "nom masculin": {
                "definitions": [
                    {
                        "definition": "Souhait de bonne journée (adressé en arrivant, en rencontrant).",
                        "example": "",
                        "synonyms": [
                            "salut"
                        ]
                    }
                ]
            }
        }
    }
]

Currently, I am storing each data's position in a variable and doing this repeatedly until I get to the position of the ultimate data I am trying to fetch like below.目前,我将每个数据的 position 存储在一个变量中并重复执行此操作,直到我到达我试图获取的最终数据的 position,如下所示。

const firstData = data[0];
const firstDataKeys = Object.keys(firstData);
const meaningsIndex = firstDataKeys.indexOf("meaning");
const meaningsData = firstData[firstDataKeys[meaningsIndex]]
const meaningsKeys = Object.keys(meaningsData);

I would like to know if there is any other way to dynamically fetch data when the desired data's position vary with each request.我想知道当所需数据的 position 随每个请求而变化时,是否还有其他方法可以动态获取数据。

You can use something like a "deep-picker" function, since the structure of the response object can change.您可以使用类似“深度选择器”function 的东西,因为响应 object 的结构可以改变。 See this comment for a clean function that would let you select the property 'definition' for example.请参阅此评论以获取干净的 function ,例如,它可以让您 select 属性“定义”。

https://stackoverflow.com/a/15643382/3347968 https://stackoverflow.com/a/15643382/3347968

Ex.前任。

const res = findNested(myResponseObj, 'definition');

Would result in something like this会导致这样的事情

res = ["Souhait de bonne journée (adressé en arrivan.."].

Javascript lets you directly parse javascript into objects with JSON.parse Javascript 让您可以使用 JSON.parse 直接将 javascript 解析为对象

So you can do something like.所以你可以做类似的事情。

const word = "exclamation"
const dict = JSON.parse(json);

//These 2 syntaxes are equivalent
const definition1 = dict["meaning"][word]["definitions"][0];
const definition2 = dict.meaning[word].definitions[0];

if you don't know if the word exists you can do something like如果你不知道这个词是否存在,你可以做类似的事情

const word = "exclamation"
const dict = JSON.parse(json);

//These 2 syntaxes are equivalent
const wordInfo = dict.meaning[word];
if(wordInfo)
{
   //you can do something more exotic, but this will only call code if the
   //word exists. 
   for(var i = 0;i < wordInfo.definitions.length;i++)
   {
     console.log(wordInfo.definitions[0])
   }
}

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

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