带有JS

时间:2018-08-02 14:27:16

标签: javascript arrays json

我正在寻找一种从JSON数据生成HTML表的方法。

由于我们使用CMS,因此我在工具选项方面受到限制,所以我只能使用JS,JQuery和ApacheVelocity(无需创建新模板,只能使用“语法”)。

好吧,我从远程API获得了这种JSON数据:

{
    "code": 0,
    "rid": "0",
    "data": {
        "subid": "-7766883411351472375",
        "data": {
            "region": {
                "123": {
                    "alias": "Europe",
                    "game": {
                        "11811809": {
                            "id": 11811809,
                            "team1_name": "Zorya Luhansk",
                            "team2_name": "SC Braga",
                            "market": {
                                "188597332": {
                                    "type": "P1XP2",
                                    "name": "Ganador del Partido",
                                    "event": {
                                        "624566458": {
                                            "price": 2.39,
                                            "name": "W1"
                                        },
                                        "624566459": {
                                            "price": 3.01,
                                            "name": "X"
                                        },
                                        "624566460": {
                                            "price": 2.82,
                                            "name": "W2"
                                        }
                                    }
                                }
                            }
                        },
                        "11811810": {
                            "id": 11811810,
                            "team1_name": "Olympiacos Piraeus",
                            "team2_name": "FC Luzern",
                            "market": {
                                "188597340": {
                                    "type": "P1XP2",
                                    "name": "Ganador del Partido",
                                    "event": {
                                        "624566476": {
                                            "price": 1.34,
                                            "name": "W1"
                                        },
                                        "624566477": {
                                            "price": 4.29,
                                            "name": "X"
                                        },
                                        "624566478": {
                                            "price": 7.92,
                                            "name": "W2"
                                        }
                                    }
                                }
                            }
                        },
                        "11844220": {
                            "id": 11844220,
                            "team1_name": "NK Domzale",
                            "team2_name": "FC Ufa",
                            "market": {
                                "189338624": {
                                    "type": "P1XP2",
                                    "name": "Ganador del Partido",
                                    "event": {
                                        "626913821": {
                                            "price": 2.35,
                                            "name": "W1"
                                        },
                                        "626913822": {
                                            "price": 2.86,
                                            "name": "X"
                                        },
                                        "626913823": {
                                            "price": 3.03,
                                            "name": "W2"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

我面临的第一个问题是那些数字索引。 引用此的唯一方法是这样的:

arr_from_json.data.data.region[123].game[11844220].team1_name

如果我们只提取几个“游戏”,甚至100个游戏,也可以。但是不可能通过不断更新的数千款游戏来使其保持更新。

在这种丑陋的JSON结构中,有什么方法可以迭代吗?

非常感谢


编辑:

我想用不同的游戏创建一张桌子:

Zorya Luhansk - SC Braga
    W1       X       W2
    2.39     3.01    2.82

对我来说,最重要的数据/关键是:球队名称,可能结果的名称和价格。

1 个答案:

答案 0 :(得分:1)

您可以使用辅助函数将这些索引对象转换为传统数组,然后在转换后以更自然的方式遍历数据。有关使用Array.map和助手function keysToArray(obj){ return Object.keys(obj).map(key => obj[key]); }

的示例,请参见下文

const resp = {
    "code": 0,
    "rid": "0",
    "data": {
        "subid": "-7766883411351472375",
        "data": {
            "region": {
                "123": {
                    "alias": "Europe",
                    "game": {
                        "11811809": {
                            "id": 11811809,
                            "team1_name": "Zorya Luhansk",
                            "team2_name": "SC Braga",
                            "market": {
                                "188597332": {
                                    "type": "P1XP2",
                                    "name": "Ganador del Partido",
                                    "event": {
                                        "624566458": {
                                            "price": 2.39,
                                            "name": "W1"
                                        },
                                        "624566459": {
                                            "price": 3.01,
                                            "name": "X"
                                        },
                                        "624566460": {
                                            "price": 2.82,
                                            "name": "W2"
                                        }
                                    }
                                }
                            }
                        },
                        "11811810": {
                            "id": 11811810,
                            "team1_name": "Olympiacos Piraeus",
                            "team2_name": "FC Luzern",
                            "market": {
                                "188597340": {
                                    "type": "P1XP2",
                                    "name": "Ganador del Partido",
                                    "event": {
                                        "624566476": {
                                            "price": 1.34,
                                            "name": "W1"
                                        },
                                        "624566477": {
                                            "price": 4.29,
                                            "name": "X"
                                        },
                                        "624566478": {
                                            "price": 7.92,
                                            "name": "W2"
                                        }
                                    }
                                }
                            }
                        },
                        "11844220": {
                            "id": 11844220,
                            "team1_name": "NK Domzale",
                            "team2_name": "FC Ufa",
                            "market": {
                                "189338624": {
                                    "type": "P1XP2",
                                    "name": "Ganador del Partido",
                                    "event": {
                                        "626913821": {
                                            "price": 2.35,
                                            "name": "W1"
                                        },
                                        "626913822": {
                                            "price": 2.86,
                                            "name": "X"
                                        },
                                        "626913823": {
                                            "price": 3.03,
                                            "name": "W2"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

function keysToArray(obj){ return Object.keys(obj).map(key => obj[key]); }

function parseGameData(data){
  return keysToArray(data.region).map(obj => keysToArray(obj.game).map(obj => {
        obj.market = keysToArray(obj.market).map(obj => {
          return {
            name: obj.name,
            event: keysToArray(obj.event)
          }
        })
        return obj
      }))
}

console.log(parseGameData(resp.data.data))