How to get Nested value Rjson

前提是你 提交于 2019-12-11 12:26:44

问题


I have data look like

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")

This data is about running application log, the data has JSON format. I just confused when I want to get sub value.

I just code like this

myData <- lapply(dRunningApplicationsProbe$value, fromJSON)

The output look like

..$ 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

In this case, Its easy for getting timestamp, and duration value

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

I have problem when I want to get mPakage value, so Anyone can help me, how to get it? Thank you..


回答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))

Output

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"



回答2:


Create a path where each element represents the name of the next level you'd like to access

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

then use this to index into the list-of-lists using [[

> 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"

A more complete solution might be

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

and

> 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"


来源:https://stackoverflow.com/questions/27227208/how-to-get-nested-value-rjson

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!