![](/img/trans.png)
[英]I'm having trouble extracting XML data from attributes and I don't know what I'm doing wrong
[英]How do I neatly output all the data from a json object if I don't know what I'm going to get back?
我是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"
}
]
}
如您所见,“结果”数组返回一个对象数组。 我想在DOM中插入每个对象的每个部分,但是每个对象没有相同的键。 有些对象有翻译,有些则有定义。 一些嵌套键甚至具有数组。 我可以编写一个函数来捕获每个对象中的所有键并返回所有信息吗?
我假设您已经使用JSON.parse()
(或其他方法)来获取实际的Object而不是问题中的JSON文本。
您可以通过多种方式执行此操作。 我建议您看一下:
确实有太多方法可以满足您的要求。 我们没有足够的信息来实际建议一种“好的”方法来做您想要的事情。 最简单的方法可能是使用将对象/ JSON映射到HTML的JavaScript模块。 这实际上取决于您最终想要得到什么。
例如,如果您将信息手动放入DOM中,则可以通过执行以下操作来确定是否具有definition
属性:
if(obj.hasOwnPrperty('definition')) {
//Yes, it has the property
}else{
//No, it does not have the property
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.