5

我怀疑我在这里遗漏了一些明显的东西,但是如何使用 R 解析深度嵌套的结构RJSONIO

例如 - 假设我想直接引用 $familyName in results.data.json$MRData$RaceTable$Races[[1]]$Results[[8]]$Driveras 使用以下代码段抓取:

require(RJSONIO)
resultsURL='http://ergast.com/api/f1/2012/1/results.json'
results.data.json=fromJSON(resultsURL)

RJSONIO似乎不想将..$Results[[i]]数据解析为结构化元素?

4

2 回答 2

3
require(RJSONIO)

somedata1<-list(a=1,b='w')
fromJSON(toJSON(somedata1))
# $a
# [1] 1

# $b
# [1] "w"

somedata2<-list(a=1,b=2)
fromJSON(toJSON(somedata2))
# a b 
# 1 2 

somedata3<-list(a='v',b='w')
fromJSON(toJSON(somedata3))
#  a   b 
# "v" "w" 

fromJSON(toJSON(somedata3),simplify=StrictNumeric)
# $a
# [1] "v"

# $b
# [1] "w"

fromJSON(toJSON(somedata2),simplify=FALSE)
# $a
# [1] 1

# $b
# [1] 2

fromJSON(toJSON(somedata3),simplifyWithNames = FALSE)
# $a
# [1] "v"

# $b
# [1] "w"


fromJSON(toJSON(somedata2),simplifyWithNames = FALSE)
# $a
# [1] 1

# $b
# [1] 2

默认情况下,从上面的示例中,RJSON 将“同质标量元素的集合/数组简化为 R 向量”。可以使用simplify或来控制这种简化simplifyWithNames。在您的示例中,您可以执行以下任何操作来访问所需的元素:

require(RJSONIO)
resultsURL='http://ergast.com/api/f1/2012/1/results.json'
results.data.json=fromJSON(resultsURL)
results.data.json$MRData$RaceTable$Races[[1]]$Results[[8]]$Driver['familyName']
# familyName 
#    "Pérez" 

results.data.json=fromJSON(resultsURL,simplify=FALSE)
results.data.json$MRData$RaceTable$Races[[1]]$Results[[8]]$Driver$familyName
# [1] "Pérez"

results.data.json=fromJSON(resultsURL,simplify=StrictNumeric)
results.data.json$MRData$RaceTable$Races[[1]]$Results[[8]]$Driver$familyName
# [1] "Pérez"

results.data.json=fromJSON(resultsURL,simplifyWithNames = FALSE)
results.data.json$MRData$RaceTable$Races[[1]]$Results[[8]]$Driver$familyName
# [1] "Pérez"
于 2012-09-17T08:03:18.123 回答
2

jsonlite包是一个分支,RJSONIO它尝试在 R 和 JSON 结构之间使用更智能的映射。我认为这可能会让你的生活更轻松:

> x = fromJSON('http://ergast.com/api/f1/2012/1/results.json')
> x$RaceTable$Races$MRData$Results[[1]]$Driver
             driverId code                                                   url
1              button  BUT            http://en.wikipedia.org/wiki/Jenson_Button
2              vettel  VET         http://en.wikipedia.org/wiki/Sebastian_Vettel
3            hamilton  HAM           http://en.wikipedia.org/wiki/Lewis_Hamilton
4              webber  WEB              http://en.wikipedia.org/wiki/Mark_Webber
5              alonso  ALO          http://en.wikipedia.org/wiki/Fernando_Alonso
6           kobayashi  KOB          http://en.wikipedia.org/wiki/Kamui_Kobayashi
7           raikkonen  RAI http://en.wikipedia.org/wiki/Kimi_R%C3%A4ikk%C3%B6nen
8               perez  PER        http://en.wikipedia.org/wiki/Sergio_P%C3%A9rez
9           ricciardo  RIC         http://en.wikipedia.org/wiki/Daniel_Ricciardo
10              resta  DIR            http://en.wikipedia.org/wiki/Paul_di_Resta
11             vergne  VER    http://en.wikipedia.org/wiki/Jean-%C3%89ric_Vergne
12            rosberg  ROS             http://en.wikipedia.org/wiki/Nico_Rosberg
13          maldonado  MAL         http://en.wikipedia.org/wiki/Pastor_Maldonado
14              glock  GLO               http://en.wikipedia.org/wiki/Timo_Glock
15                pic  PIC              http://en.wikipedia.org/wiki/Charles_Pic
16        bruno_senna  SEN              http://en.wikipedia.org/wiki/Bruno_Senna
17              massa  MAS             http://en.wikipedia.org/wiki/Felipe_Massa
18         kovalainen  KOV        http://en.wikipedia.org/wiki/Heikki_Kovalainen
19             petrov  PET            http://en.wikipedia.org/wiki/Vitaly_Petrov
20 michael_schumacher  MSC       http://en.wikipedia.org/wiki/Michael_Schumacher
21           grosjean  GRO          http://en.wikipedia.org/wiki/Romain_Grosjean
22         hulkenberg  HUL     http://en.wikipedia.org/wiki/Nico_H%C3%BClkenberg
23               rosa  DLR         http://en.wikipedia.org/wiki/Pedro_de_la_Rosa
24        karthikeyan  KAR       http://en.wikipedia.org/wiki/Narain_Karthikeyan
   givenName  familyName dateOfBirth nationality
1     Jenson      Button  1980-01-19     British
2  Sebastian      Vettel  1987-07-03      German
3      Lewis    Hamilton  1985-01-07     British
4       Mark      Webber  1976-08-27  Australian
5   Fernando      Alonso  1981-07-29     Spanish
6      Kamui   Kobayashi  1986-09-13    Japanese
7       Kimi   Räikkönen  1979-10-17     Finnish
8     Sergio       Pérez  1990-01-26     Mexican
9     Daniel   Ricciardo  1989-07-01  Australian
10      Paul    di Resta  1986-04-16    Scottish
11 Jean-Éric      Vergne  1990-04-25      French
12      Nico     Rosberg  1985-06-27      German
13    Pastor   Maldonado  1985-03-09  Venezuelan
14      Timo       Glock  1982-03-18      German
15   Charles         Pic  1990-02-15      French
16     Bruno       Senna  1983-10-15   Brazilian
17    Felipe       Massa  1981-04-25   Brazilian
18    Heikki  Kovalainen  1981-10-19     Finnish
19    Vitaly      Petrov  1984-09-08     Russian
20   Michael  Schumacher  1969-01-03      German
21    Romain    Grosjean  1986-04-17      French
22      Nico  Hülkenberg  1987-08-19      German
23     Pedro  de la Rosa  1971-02-24     Spanish
24    Narain Karthikeyan  1977-01-14      Indian
于 2014-02-22T08:39:42.473 回答