我在处理非常讨厌的嵌套 JSON 时遇到了麻烦。
格式是这样的
{
"matches": [
{
"matchId": 1,
"region": "BR",
"participants": [
{
"participantId": 0,
"teamId": 200,
"stats": {
"winner": true,
"champLevel": 16,
"item0": 3128,
}
{
"matchId": 2,
"region": "BR",
"participants": [
{
"participantId": 0,
"teamId": 201,
"stats": {
"winner": false,
"champLevel": 18,
"item0": 3128,
"item1": 3157,
"item1": 3158,
}
正如您在第二场比赛中看到的项目数量增加了,但在数据框中第一行将具有相同的列:
MatchId region ... stats.winner stats.champLevel stats.item0 stats.item1 stats.item2
1 BR TRUE 16 3128 1 BR
1 BR TRUE 16 3128 3157 3158
看到第一行小于第二行,所以 R 回收这些值......
如果您想要完整的数据,可以在以下位置获取:http://pastebin.com/HQDf2ase
我如何将 json 解析为 data.frame:
json.matchData <- fromJSON(file="file.json"))
取消列出 Json 的元素并将其转换为数据框
matchData.i <- lapply(json.matchData$matches, function(x){ unlist(x)})
转换成数据框
matchData <- do.call("rbind", matchData.i)
matchData <- as.data.frame(matchData)
但是数据帧很混乱,因为有些字段应该是 NA,但它们填充了错误的值。