这是我的 JSON 数据:
{
"main": [
{
"main_id": "1234",
"main_sub": [
{
"main_info": {
"main_1" : "A",
"main_2" : "B",
"main_3" : "C"
},
"main_4" : "D",
"main_5" : "E",
},
{
"main_6" : "F",
"main_7" : "G",
"main_info": {
"main_1" : "H",
"main_2" : "I",
"main_3" : "J"
},
"main_4" : "D",
"main_5" : "E",
"main_act": [
{
"main_act_1": 12,
"main_act_1_1": "C",
"main_act_1_2": "T"
},
{
"main_act_1": 12,
"main_act_1_1": "D",
"main_act_1_2": "T"
}
],
"main_8" : "R"
},
{
"main_4" : "D",
"main_5" : "E",
"main_10": "P"
}
],
"main_score": 0.1
},
{
"main_id": "1334",
"main_sub": [
{
"main_info": {
"main_1" : "B",
"main_2" : "Q",
"main_3" : "C",
"main_11": "D"
},
"main_4" : "S",
"main_5" : "E",
"main_6" : "G",
"main_7" : "T"
},
{
"main_6" : "F",
"main_7" : "G",
"main_info": {
"main_1" : "A",
"main_2" : "I",
"main_3" : "J"
},
"main_10": "W",
"main_4" : "D",
"main_5" : "E",
"main_act": [
{
"main_act_1": 112,
"main_act_1_1": "C",
"main_act_1_2": "T"
},
{
"main_act_1": 123,
"main_act_1_1": "D",
"main_act_1_2": "T"
}
],
"main_8" : "R"
},
{
"main_4" : "D",
"main_5" : "E",
"main_10": "P"
}
],
"main_score": 0.1
}
],
"main_count": 2
}
由于嵌套,即使在 jQuery 中使用 $.each 后,我也无法读取大部分数据。
还有一种方法可以读取此 json 数据的每一行并将其打印在屏幕上,而无需在我的 jQuery 代码中实际提及“main_1”之类的字段名称?
预期输出:
我只想为 main 的两个实例中的每一个打印字段名称及其值。
有点像:
main_id - 1234
main_sub -
main_info -
main_1 - A
main_2 - B
等等......................(对于 main 的两个实例 - 因此 main_count = 2 在 json 数据的末尾)