首页 > 解决方案 > 如何使用 Rstudio 解析数据框中的 Json 列

问题描述

我正在尝试在我的数据框列中解析 json 格式。列名是属性。这是示例:

{"player_version":"vjs-core-6.9.0-hls-5.8.3-ads-5.1.5-ima-2f3a06d-dash-2.9.3","seconds":0.1,"device_model":"其他" ,"levels":4,"referrer":" https://www.bola.net/ ","os_version":"7","message":"由于网络错误,无法从服务器请求广告。", “类型”:“视频”,“autoplay_data”:true,“target_bitrate”:600,“os_name”:“Windows”,“video”:true,“ad_tag”:”https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/95250053/VIDIO_DESKTOP/PREROLL_KLY&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&url=[referrer_url]&description_url=[description_url]&correlator=[timestamp] &cust_params=vidio_embed%3Dtrue%26vidio_user%3Dkapanlagi%26vidio_embed_site%3Dwww.bola.net%26vidio_id%3D1506205%26vidio_categories%3Dentertainment%26vidio_tag%3Dultah-kim-kardashian%2Cthe-kardashians-liburan-di-bali%2Ckapanlagi-holwood", "平台":"web-desktop", "video_id":1506205,"cf":"ccfbfd463b8b703bee7fe656073f6299", "current_time":5.319523,"level":2,"mse_vp9":true,"visitor_id":"b8f98cdb -d7d6-4f63-b3ac-a73c854a0858", "ad_type":"banner","webm_vp8":true,"device_vendor":{},"mse_h264":true,"h264":true,"browser_name":"Firefox" ,"vjs_html5":true,"mse":true,"autoplay":true,"setup_time":0.1,"play_uuid":"716f7fa9-d049-471b-b7de-79df8a2cdccd", "autoplay_supported":true,"vjs_mpegurl" :false,"app_name":"vidio","video_duration":84,"browser_version":"63.0","player_name":“videojs”,“batch_uuid”:“7d101615-c1a9-449f-aa96-bb0aa6330d38”,“嵌入”:“true”,“uuid”:“f41a01c3-f8f3-4295-b33c-9873e490c59c”,“登录”:false, "batch_full":false,"percentage":10,"ad_uuid":"9eede46b-345e-42e1-89a5-6460df8d8aeb","supported":true,"flash_version":"11,4,402","has_ad":true}“has_ad”:真}“has_ad”:真}

我已经使用了 rjson/jsonlite 库,但它根本不起作用。我有这样的错误消息:
FUN(X[[i]], ...) 中的错误:STRING_ELT() 只能应用于“字符向量”,而不是“整数”

我的代码:

library('rjson')
new <- do.call(rbind.data.frame, lapply(all_files$properties, rjson::fromJSON))

标签: rjson

解决方案


我更喜欢jsonlite这种类型的问题的包。

library(jsonlite)

#convert the JSON into a list of names/values
values<-fromJSON('{"player_version":"vjs-core-6.9.0-hls-5.8.3-ads-5.1.5-ima-2f3a06d-dash-2.9.3","seconds":0.1,"device_model":"Other","levels":4,"referrer":"https://www.bola.net/","os_version":"7","message":"Unable to request ads from server due to network error.","type":"Video","autoplay_data":true,"target_bitrate":600,"os_name":"Windows","video":true,"ad_tag":"","platform":"web-desktop","video_id":1506205,"cf":"ccfbfd463b8b703bee7fe656073f6299","current_time":5.319523,"level":2,"mse_vp9":true,"visitor_id":"b8f98cdb-d7d6-4f63-b3ac-a73c854a0858","ad_type":"banner","webm_vp8":true,"device_vendor":{},"mse_h264":true,"h264":true,"browser_name":"Firefox","vjs_html5":true,"mse":true,"autoplay":true,"setup_time":0.1,"play_uuid":"716f7fa9-d049-471b-b7de-79df8a2cdccd","autoplay_supported":true,"vjs_mpegurl":false,"app_name":"vidio","video_duration":84,"browser_version":"63.0","player_name":"videojs","batch_uuid":"7d101615-c1a9-449f-aa96-bb0aa6330d38","embed":"true","uuid":"f41a01c3-f8f3-4295-b33c-9873e490c59c","login":false,"batch_full":false,"percentage":10,"ad_uuid":"9eede46b-345e-42e1-89a5-6460df8d8aeb","supported":true,"flash_version":"11,4,402","has_ad":true}')

#convert the list into a data frame where each row is a parameter.
df<-as.data.frame(unlist(values))

推荐阅读