如果我不知道我将返回什么,我如何整齐地输出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?
我是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
}
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 我应该如何将响应数据保存在对象(json)中以获得更好的操作和性能
- 用javascript从列表对象(JSON的)构建diffrent选项卡
- php请求带有多个对象json-jquery
- AngularJS,过滤器:如何将一个巨大的对象(JSON)变成一个数组
- 动态属性对象 - JSON
- JavaScript 对象 (JSON) 中的动态设置值
- 从数组查询获取对象 JSON 值时出错
- 使用jQuerygetJSON将多维对象JSON转换为HTML
- 需要从 Angular JS 应用程序中的另一个 JSON 对象数组填充 JSON 数组的每个对象 JSON 对象数组
- 嵌套对象JSON排序JavaScript
- PHP在MySQL中保存来自Javascript的对象"JSON.stringify"通过Ajax
- 将JavaScript对象/ JSON转换为PHP数组
- JSON对象.JSON对象内部
- 访问对象的对象- JSON API
- 数组到对象JSON
- Message":"传入的无效对象(JSON数据格式化问题)