如果我不知道我将返回什么,我如何整齐地输出json对象中的所有数据?

How do I neatly output all the data from a json object if I don't know what I'm going to get back?

本文关键字:对象 json 数据 输出 返回 我不知道 什么 何整齐 如果      更新时间:2023-09-26

我是Pearson的字典api。下面是我从api调用中得到的一个示例:

{
  "status": 200,
  "offset": 0,
  "limit": 10,
  "count": 10,
  "total": 135,
  "url": "/v2/dictionaries/entries?headword=dog",
  "results": [
    {
      "datasets": [
        "lasde",
        "dictionary"
      ],
      "headword": "dog",
      "homnum": 2,
      "id": "cqAFksnRpT",
      "part_of_speech": "verb",
      "senses": [
        {
          "definition": [
            "if a problem dogs you, it causes trouble for a long time"
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/cqAFksnRpT"
    },
    {
      "datasets": [
        "ldec",
        "dictionary"
      ],
      "headword": "dogged",
      "id": "cqAJ3cApA5",
      "part_of_speech": "adjective",
      "senses": [
        {
          "translation": "堅持不懈的,頑強的,不屈不撓的"
        }
      ],
      "url": "/v2/dictionaries/entries/cqAJ3cApA5"
    },
    {
      "datasets": [
        "laad3",
        "dictionary"
      ],
      "headword": "dog",
      "homnum": 1,
      "id": "cs1sPF57rs",
      "part_of_speech": "noun",
      "senses": [
        {
          "definition": "a very common animal with four legs that is often kept as a pet or used for guarding buildings",
          "examples": [
            {
              "text": "I could hear a dog barking."
            }
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/cs1sPF57rs"
    },
    {
      "datasets": [
        "laes",
        "dictionary"
      ],
      "headword": "dog",
      "id": "csTMcVNVB7",
      "part_of_speech": "verb",
      "senses": [
        {
          "definition": [
            "if a problem or bad luck dogs you, it continues to causes trouble for a long time"
          ],
          "translations": [
            {
              "example": [
                {
                  "text": "Funding problems have dogged the project from the start.",
                  "translation": {
                    "text": [
                      "Los problemas de financiación han marcado el proyecto desde su inicio."
                    ]
                  }
                }
              ],
              "full_form_tran": [
                "mala suerte, problemas"
              ],
              "text": [
                "perseguir"
              ]
            }
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/csTMcVNVB7"
    },
    {
      "datasets": [
        "brep",
        "dictionary"
      ],
      "headword": "dogged",
      "id": "ct59x9PnK6",
      "part_of_speech": "adjective",
      "pronunciations": [
        {
          "ipa": "ˈdɔgɪd",
          "lang": "American English"
        }
      ],
      "senses": [
        {
          "translations": [
            {
              "example": [
                {
                  "text": "She got there through sheer dogged determination to succeed.",
                  "translation": {
                    "text": [
                      "Ela chegou lá somente por uma determinação obstinada de ter sucesso."
                    ]
                  }
                }
              ],
              "text": [
                "obstinado"
              ]
            }
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/ct59x9PnK6"
    },
    {
      "datasets": [
        "laes",
        "dictionary"
      ],
      "headword": "doggedly",
      "id": "csTMcVNjmK",
      "part_of_speech": "adverb",
      "pronunciations": [
        {
          "ipa": "ˈdɔgɪdli"
        }
      ],
      "senses": [
        {
          "definition": [
            "in a way that shows you are determined to achieve something or continue doing something despite difficulties"
          ],
          "translations": [
            {
              "example": [
                {
                  "text": "He spent the next two years doggedly following his plan.",
                  "translation": {
                    "text": [
                      "Pasó los dos años siguientes apegado obstinadamente a su plan."
                    ]
                  }
                }
              ],
              "text": [
                "con tenacidad"
              ]
            }
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/csTMcVNjmK"
    },
    {
      "datasets": [
        "ldoce5",
        "dictionary"
      ],
      "headword": "dog",
      "homnum": 2,
      "id": "cqAFAggc8c",
      "part_of_speech": "verb",
      "senses": [
        {
          "definition": [
            "if a problem or bad luck dogs you, it causes trouble for a long time"
          ],
          "examples": [
            {
              "audio": [
                {
                  "type": "example",
                  "url": "/v2/dictionaries/assets/ldoce/exa_pron/p008-000811030.mp3"
                }
              ],
              "text": "He has been dogged by injury all season."
            }
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/cqAFAggc8c"
    },
    {
      "datasets": [
        "lasde",
        "dictionary"
      ],
      "headword": "dog",
      "homnum": 1,
      "id": "cqAFksYTtm",
      "part_of_speech": "noun",
      "pronunciations": [
        {
          "ipa": "dɒg"
        },
        {
          "ipa": "dɒːg",
          "lang": "American English"
        }
      ],
      "senses": [
        {
          "cross_references": [
            {
              "headword": "pet",
              "homnum": 1,
              "url": "/v2/dictionaries/entries/cqAFxHe1mp"
            }
          ],
          "definition": [
            "a very common animal with four legs, fur, and a tail. Dogs are often kept as pets or as working animals."
          ],
          "examples": [
            {
              "text": "I could hear a dog barking."
            }
          ],
          "related_words": [
            "puppy"
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/cqAFksYTtm"
    },
    {
      "datasets": [
        "laad3",
        "dictionary"
      ],
      "headword": "dogged",
      "id": "cs1sPG5Mck",
      "part_of_speech": "adjective",
      "senses": [
        {
          "definition": "dogged actions or behavior show that you are very determined to continue doing something",
          "examples": [
            {
              "text": "a dogged determination to succeed"
            }
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/cs1sPG5Mck"
    },
    {
      "datasets": [
        "leasd",
        "dictionary"
      ],
      "headword": "dog",
      "id": "crzpqPP3Zh",
      "part_of_speech": "noun",
      "senses": [
        {
          "definition": "huisdier",
          "translations": [
            {
              "text": "hond"
            }
          ]
        }
      ],
      "url": "/v2/dictionaries/entries/crzpqPP3Zh"
    }
  ]
}

可以看到,"results"数组返回一个对象数组。我想在DOM中插入每个对象的每个部分,但每个对象的键不相同。有些对象有翻译,有些对象有定义。一些嵌套的键甚至有数组。是否有一个函数,我可以写捕捉所有的键在每个对象,并返回所有的信息?

我假设您已经使用JSON.parse()(或其他方法)来获得实际的对象,而不是您问题中的JSON文本。

有多种方法可以做到这一点。我建议你看一下:

  • 对象的各种方法例如:
    • keys()
    • getOwnPropertyNames()
    • hasOwnProperty()
  • for...in
  • for...of
  • Array.prototype.forEach()(例如与keys()组合使用)
  • typeof

真的有太多的方法来完成你所要求的。我们没有足够的信息来建议一个"好"的方法来做你想做的事。最简单的方法可能是使用JavaScript模块将对象/JSON映射到HTML。这完全取决于你最终想要得到什么。

如果您手动将信息放入DOM中,您可以,例如,确定您是否有definition属性,执行:

if(obj.hasOwnPrperty('definition')) {
    //Yes, it has the property
}else{
    //No, it does not have the property
}