如何获得嵌套值Rjson

时间:2014-12-01 11:16:44

标签: r rjson

我的数据看起来像

structure(list(`_id` = c(2653L, 2729L, 2920L, 2921L, 2922L, 2923L
), name = c("RunningApplicationsProbe", "RunningApplicationsProbe", 
"RunningApplicationsProbe", "RunningApplicationsProbe", "RunningApplicationsProbe", 
"RunningApplicationsProbe"), timestamp = c(1404116791.097, 1404116803.554, 
1404116805.61, 1404116814.795, 1404116830.116, 1404116868.244
), value = c("{\"duration\":12.401,\"taskInfo\":{\"baseIntent\":{\"mAction\":\"android.intent.action.MAIN\",\"mCategories\":[\"android.intent.category.LAUNCHER\"],\"mComponent\":{\"mClass\":\"kr.ac.jnu.netsys.MainActivity\",\"mPackage\":\"edu.mit.media.funf.wifiscanner\"},\"mFlags\":268435456,\"mPackage\":\"edu.mit.media.funf.wifiscanner\",\"mWindowMode\":0},\"id\":102,\"persistentId\":102},\"timestamp\":1404116791.097}", 
"{\"duration\":2.055,\"taskInfo\":{\"baseIntent\":{\"mAction\":\"android.intent.action.MAIN\",\"mCategories\":[\"android.intent.category.LAUNCHER\"],\"mComponent\":{\"mClass\":\"com.nhn.android.search.ui.pages.SearchHomePage\",\"mPackage\":\"com.nhn.android.search\"},\"mFlags\":270532608,\"mWindowMode\":0},\"id\":97,\"persistentId\":97},\"timestamp\":1404116803.554}", 
"{\"duration\":9.183,\"taskInfo\":{\"baseIntent\":{\"mAction\":\"android.intent.action.MAIN\",\"mCategories\":[\"android.intent.category.HOME\"],\"mComponent\":{\"mClass\":\"com.buzzpia.aqua.launcher.LauncherActivity\",\"mPackage\":\"com.buzzpia.aqua.launcher\"},\"mFlags\":274726912,\"mWindowMode\":0},\"id\":3,\"persistentId\":3},\"timestamp\":1404116805.61}", 
"{\"duration\":15.320,\"taskInfo\":{\"baseIntent\":{\"mAction\":\"android.intent.action.MAIN\",\"mCategories\":[\"android.intent.category.LAUNCHER\"],\"mComponent\":{\"mClass\":\"kr.ac.jnu.netsys.MainActivity\",\"mPackage\":\"edu.mit.media.funf.wifiscanner\"},\"mFlags\":270532608,\"mWindowMode\":0},\"id\":103,\"persistentId\":103},\"timestamp\":1404116814.795}", 
"{\"duration\":38.126,\"taskInfo\":{\"baseIntent\":{\"mComponent\":{\"mClass\":\"com.rechild.advancedtaskkiller.AdvancedTaskKiller\",\"mPackage\":\"com.rechild.advancedtaskkiller\"},\"mFlags\":71303168,\"mWindowMode\":0},\"id\":104,\"persistentId\":104},\"timestamp\":1404116830.116}", 
"{\"duration\":3.015,\"taskInfo\":{\"baseIntent\":{\"mAction\":\"android.intent.action.MAIN\",\"mCategories\":[\"android.intent.category.LAUNCHER\"],\"mComponent\":{\"mClass\":\"kr.ac.jnu.netsys.MainActivity\",\"mPackage\":\"edu.mit.media.funf.wifiscanner\"},\"mFlags\":270532608,\"mWindowMode\":0},\"id\":103,\"persistentId\":103},\"timestamp\":1404116868.244}"
)), .Names = c("_id", "name", "timestamp", "value"), row.names = c(2653L, 
2729L, 2920L, 2921L, 2922L, 2923L), class = "data.frame")

此数据是关于运行应用程序日志的,数据具有JSON格式。 当我想获得子值时,我只是困惑。

我只是像这样编码

myData <- lapply(dRunningApplicationsProbe$value, fromJSON)

输出看起来像

..$ duration : num 9.18
  ..$ taskInfo :List of 3
  .. ..$ baseIntent  :List of 5
  .. .. ..$ mAction    : chr "android.intent.action.MAIN"
  .. .. ..$ mCategories: chr "android.intent.category.HOME"
  .. .. ..$ mComponent :List of 2
  .. .. .. ..$ mClass  : chr "com.buzzpia.aqua.launcher.LauncherActivity"
  .. .. .. ..$ mPackage: chr "com.buzzpia.aqua.launcher"
  .. .. ..$ mFlags     : num 2.75e+08
  .. .. ..$ mWindowMode: num 0
  .. ..$ id          : num 3
  .. ..$ persistentId: num 3
  ..$ timestamp: num 1.4e+09

在这种情况下,很容易获得时间戳和持续时间值

dRunningApplicationsProbe$time <- do.call(rbind,lapply(myData, `[` ,'timestamp'))  

当我想获得mPakage值时,我有问题,所以任何人都可以帮助我,如何获得它? 谢谢..

2 个答案:

答案 0 :(得分:1)

df <- structure(list(`_id` = c(2653L, 2729L, 2920L, 2921L, 2922L, 2923L
), name = c("RunningApplicationsProbe", "RunningApplicationsProbe", 
            "RunningApplicationsProbe", "RunningApplicationsProbe", "RunningApplicationsProbe", 
            "RunningApplicationsProbe"), timestamp = c(1404116791.097, 1404116803.554, 
                                                       1404116805.61, 1404116814.795, 1404116830.116, 1404116868.244
            ), value = c("{\"duration\":12.401,\"taskInfo\":{\"baseIntent\":{\"mAction\":\"android.intent.action.MAIN\",\"mCategories\":[\"android.intent.category.LAUNCHER\"],\"mComponent\":{\"mClass\":\"kr.ac.jnu.netsys.MainActivity\",\"mPackage\":\"edu.mit.media.funf.wifiscanner\"},\"mFlags\":268435456,\"mPackage\":\"edu.mit.media.funf.wifiscanner\",\"mWindowMode\":0},\"id\":102,\"persistentId\":102},\"timestamp\":1404116791.097}", 
                         "{\"duration\":2.055,\"taskInfo\":{\"baseIntent\":{\"mAction\":\"android.intent.action.MAIN\",\"mCategories\":[\"android.intent.category.LAUNCHER\"],\"mComponent\":{\"mClass\":\"com.nhn.android.search.ui.pages.SearchHomePage\",\"mPackage\":\"com.nhn.android.search\"},\"mFlags\":270532608,\"mWindowMode\":0},\"id\":97,\"persistentId\":97},\"timestamp\":1404116803.554}", 
                         "{\"duration\":9.183,\"taskInfo\":{\"baseIntent\":{\"mAction\":\"android.intent.action.MAIN\",\"mCategories\":[\"android.intent.category.HOME\"],\"mComponent\":{\"mClass\":\"com.buzzpia.aqua.launcher.LauncherActivity\",\"mPackage\":\"com.buzzpia.aqua.launcher\"},\"mFlags\":274726912,\"mWindowMode\":0},\"id\":3,\"persistentId\":3},\"timestamp\":1404116805.61}", 
                         "{\"duration\":15.320,\"taskInfo\":{\"baseIntent\":{\"mAction\":\"android.intent.action.MAIN\",\"mCategories\":[\"android.intent.category.LAUNCHER\"],\"mComponent\":{\"mClass\":\"kr.ac.jnu.netsys.MainActivity\",\"mPackage\":\"edu.mit.media.funf.wifiscanner\"},\"mFlags\":270532608,\"mWindowMode\":0},\"id\":103,\"persistentId\":103},\"timestamp\":1404116814.795}", 
                         "{\"duration\":38.126,\"taskInfo\":{\"baseIntent\":{\"mComponent\":{\"mClass\":\"com.rechild.advancedtaskkiller.AdvancedTaskKiller\",\"mPackage\":\"com.rechild.advancedtaskkiller\"},\"mFlags\":71303168,\"mWindowMode\":0},\"id\":104,\"persistentId\":104},\"timestamp\":1404116830.116}", 
                         "{\"duration\":3.015,\"taskInfo\":{\"baseIntent\":{\"mAction\":\"android.intent.action.MAIN\",\"mCategories\":[\"android.intent.category.LAUNCHER\"],\"mComponent\":{\"mClass\":\"kr.ac.jnu.netsys.MainActivity\",\"mPackage\":\"edu.mit.media.funf.wifiscanner\"},\"mFlags\":270532608,\"mWindowMode\":0},\"id\":103,\"persistentId\":103},\"timestamp\":1404116868.244}"
            )), .Names = c("_id", "name", "timestamp", "value"), row.names = c(2653L, 
                                                                               2729L, 2920L, 2921L, 2922L, 2923L), class = "data.frame")


myData <- lapply(df$value, fromJSON)
time <- do.call(rbind,lapply(myData, function(x) x$timestamp))
mPakage <- do.call(rbind,lapply(myData, function(x) x$taskInfo$baseIntent$mComponent$mPackage))

输出

new_df <- cbind(time,mPakage)

new_df
     [,1]             [,2]                            
[1,] "1404116791.097" "edu.mit.media.funf.wifiscanner"
[2,] "1404116803.554" "com.nhn.android.search"        
[3,] "1404116805.61"  "com.buzzpia.aqua.launcher"     
[4,] "1404116814.795" "edu.mit.media.funf.wifiscanner"
[5,] "1404116830.116" "com.rechild.advancedtaskkiller"
[6,] "1404116868.244" "edu.mit.media.funf.wifiscanner"

答案 1 :(得分:1)

创建一个路径,其中每个元素代表您要访问的下一个级别的名称

path = c("taskInfo", "baseIntent", "mComponent", "mPackage")

然后使用它来使用[[

索引到列表列表中
> sapply(myData, "[[", path)
[1] "edu.mit.media.funf.wifiscanner" "com.nhn.android.search"        
[3] "com.buzzpia.aqua.launcher"      "edu.mit.media.funf.wifiscanner"
[5] "com.rechild.advancedtaskkiller" "edu.mit.media.funf.wifiscanner"

更完整的解决方案可能是

paths = list(timestamp="timestamp",
             mPackage=c("taskInfo", "baseIntent", "mComponent", "mPackage"))

> sapply(paths, function(path) sapply(myData, "[[", path))
     timestamp        mPackage                        
[1,] "1404116791.097" "edu.mit.media.funf.wifiscanner"
[2,] "1404116803.554" "com.nhn.android.search"        
[3,] "1404116805.61"  "com.buzzpia.aqua.launcher"     
[4,] "1404116814.795" "edu.mit.media.funf.wifiscanner"
[5,] "1404116830.116" "com.rechild.advancedtaskkiller"
[6,] "1404116868.244" "edu.mit.media.funf.wifiscanner"