简体   繁体   中英

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

I'm Pearson's dictionary api. Here is an example of what I get back from an api call:

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

As you can see, the "results" array returns an array of objects. I want insert each part of each object in my DOM, but each object doesn't have the same keys. Some objects have a translation, while some have a definition. Some nested keys even have arrays. Is there a function I can write to catch all the keys in each object and return all the info?

I'm assuming that you have used JSON.parse() (or other methodology) to get an actual Object instead of the JSON text that is in your question.

There are multiple ways that you can do this. I would suggest that you look at:

There are really just too many ways to do what you are asking. We don't have enough information to be able to actually suggest a "good" method of doing what you desire. The easiest way may be to use a JavaScript module that does a mapping of an object/JSON to HTML. It really depends on what you are actually wanting to end up with.

If you are manually putting the information in your DOM, you could, for instance, determine if you have a definition property, by doing:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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