Regex 对于许多记录,使用str replace删除json文档中的部分字符串
我想替换此文件中导致json参数无效的字符串。我可以手动删除第一个字符串“_id”:ObjectId(“539163D7BD35003”),并可以将此json转换为数据帧。有没有办法用str_replace之类的函数替换json文件的所有实例。我尝试了以下方法,但没有成功。有什么建议吗Regex 对于许多记录,使用str replace删除json文档中的部分字符串,regex,r,string-matching,stringr,Regex,R,String Matching,Stringr,我想替换此文件中导致json参数无效的字符串。我可以手动删除第一个字符串“_id”:ObjectId(“539163D7BD35003”),并可以将此json转换为数据帧。有没有办法用str_replace之类的函数替换json文件的所有实例。我尝试了以下方法,但没有成功。有什么建议吗 library(RJSONIO) library(stringr) json_file<- '{ "_id" : ObjectId( "539163d7bd350003" ), "login" :
library(RJSONIO)
library(stringr)
json_file<- '{ "_id" : ObjectId( "539163d7bd350003" ), "login" : "vui", "id" : 369607, "avatar_url" : "https://avatars.mashupsusercontent.com/u/369607?", "gravatar_id" : "df8897ffebe16c5b0cd690925c63e190", "url" : "https://api.mashups.com/users/vui", "html_url" : "https://mashups.com/vui", "followers_url" : "https://api.mashups.com/users/vui/followers", "following_url" : "https://api.mashups.com/users/vui/following{/other_user}", "gists_url" : "https://api.mashups.com/users/vui/gists{/gist_id}", "starred_url" : "https://api.mashups.com/users/vui/starred{/owner}{/repo}", "subscriptions_url" : "https://api.mashups.com/users/vui/subscriptions", "organizations_url" : "https://api.mashups.com/users/vui/orgs", "repos_url" : "https://api.mashups.com/users/vui/repos", "events_url" : "https://api.mashups.com/users/vui/events{/privacy}", "received_events_url" : "https://api.mashups.com/users/vui/received_events", "type" : "User", "site_admin" : false, "org" : "amurath" }'
str_replace(json_file,"_id*" , "")
json_file <- fromJSON(json_file)
json_file <- lapply(json_file, function(x) {
x[sapply(x, is.null)] <- NA
unlist(x)
})
df<- do.call("rbind", json_file)
df<- data.frame(json_file)
库(RJSONIO)
图书馆(stringr)
json_file您可以使用以下内容:
str_replace(json_file,"\"_id[^,]*,\\s*" , "")
请参阅@akrun无需str\u replace\u all
。。由于id
per-doc(mongodb)@karthikmanchala有一次,这只是一个猜测,因为OP的代码与示例一起工作,问题在于字符串本身。我可以用你提到的函数替换“login”这样的词,但我想用“”替换这种类型的字符串“”_id“:ObjectId(“539163D7BD35003”),“我在ObjectId中有数字,这些数字会随着记录不断变化!是的,它工作得很好!我需要检查多个记录@karthikmanchala我犯了这个错误!错误:“\s”是字符串中无法识别的转义,该字符串以“\”\u id[^,]*,\s“我正在尝试执行[链接]()以解决另一个问题。”。我不能在R str_替换中直接使用它。你能建议str_替换所有(json_文件,“\”,{author[^*]*],\\s*”,“”)吗?