我有一个数据,其中一列为 json 字符串:
reservation reasons
1592 [{"name"=>"jorge", "value"=>"MX"}, {"name"=>"Billing phone number", "value"=>"1123"}, {"name"=>"BillingCountry", "value"=>"USA"}]
1597 [{"name"=>"BillingAddress_Country", "value"=>"IN"}, {"name"=>"Billing phone number country code", "value"=>"IN"}, {"name"=>"Latest amount", "value"=>"583000000"}]
Run Code Online (Sandbox Code Playgroud)
我想按如下方式解析该列:
reservation name value
1592 jorge mx
1592 Billing phone number 1123
1592 BillingCountry USA
1597 BillingAddress_Country IN
1597 Billing phone number country code IN
1597 Latest amount 583000000
Run Code Online (Sandbox Code Playgroud)
我在 R 中使用 jsonlite。我的代码中出现以下错误:
data<-read.csv("data.csv")
json<-data$reasons
mydf <- fromJSON(json)
Error: Argument 'txt' must be a JSON string, URL or file.
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我在哪里犯了错误?我需要做哪些修改?提前谢谢了!
dat%>%
mutate(reasons=str_split(gsub("[^=A-Za-z,0-9{} ]+","",reasons),"(?<=\\}),\\s*"))%>%
unnest()%>%
mutate(names=str_extract(reasons,"(?<=name=)[^,}]+"),
values=str_extract(reasons,"(?<=value=)[^,}]+"),
reasons=NULL)
reservation names values
1 1592 jorge MX
2 1592 Billing phone number 1123
3 1592 BillingCountry USA
4 1597 BillingAddressCountry IN
5 1597 Billing phone number country code IN
6 1597 Latest amount 583000000
Run Code Online (Sandbox Code Playgroud)
使用此代码,如果您需要电子邮件,只需添加email=str_extract..
等
归档时间: |
|
查看次数: |
9827 次 |
最近记录: |