迭代一个列表然后解析JSON:词法错误?

时间:2016-01-26 02:50:15

标签: r

我试图解析一下steam API。首先,我构建一个给定蒸汽组的用户ID列表:

groupdata <- xmlParse("http://steamcommunity.com/groups/dogeLAN/memberslistxml?xml=1")

uids <- xmlToDataFrame(
  getNodeSet(groupdata, "//steamID64"),
  colClasses=c("character")
  )

我想迭代用户ID并将数据解析为收集的数据框:

gamedata_final <- data.frame(NULL)
for(i in uids){

  gamedata <- fromJSON(paste("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=[key removed for security]&steamid=",i,"&format=json&include_played_free_games=1", sep=""))

  gamedata_df <- data.frame(gamedata)
  gamedata_df$UID <- c("76561197988364229")
  gamedata_df$response.game_count <- NULL

  gamedata_final <- rbind(gamedata_df, gamedata_final)

}

paste()函数打印出我想要的URL,这些URL在单独运行时(循环外)起作用:

paste("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=&steamid=",i,"&format=json&include_played_free_games=1", sep="")
[1] "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=&steamid=76561197988364229&format=json&include_played_free_games=1"
 [2] "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=&steamid=76561197990340675&format=json&include_played_free_games=1"
 [3] "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=&steamid=76561197985004130&format=json&include_played_free_games=1"

然而,当我尝试运行循环时,我收到一个错误:

Error: lexical error: invalid char in json text.
                                   http://api.steampowered.com/IPl
                 (right here) ------^

我觉得我接近JSON解析错误的粘贴,但我不确定到底在哪里。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您需要从网址下载内容。 尝试使用RCurl库中的getURL():

gamedata <- fromJSON(getURL(paste("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=[key removed for security]&steamid=",i,"&format=json&include_played_free_games=1", sep="")))