Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解析多个JSON字符串并使用R转换为数据帧行?_R_Json - Fatal编程技术网

如何解析多个JSON字符串并使用R转换为数据帧行?

如何解析多个JSON字符串并使用R转换为数据帧行?,r,json,R,Json,你好社区我是新来的stackoverflow希望我能正确地写这个问题 我有一个R数据帧,看起来像这样: ID|Product1|Type |Product_JSON_string 1 | Bread |Grocery |{"ID":"1","Product1":"Bread","Type":"Automotive"} 2 | Butter |Grocery |{&

你好社区我是新来的stackoverflow希望我能正确地写这个问题

我有一个R数据帧,看起来像这样:

ID|Product1|Type     |Product_JSON_string
1 | Bread  |Grocery  |{"ID":"1","Product1":"Bread","Type":"Automotive"}
2 | Butter |Grocery  |{"ID":"2","Product1":"Butter","Type":"Grocery"} 
df <- data.frame(ID  = c("1", "2"),
                  Product1 = c("Bread", "Butter"),
                  Type= c("Grocery", "Grocery"),
                  Product_JSON_string= c('{"ID":"1","Product1":"Bread","Type":"Automotive"}',
                                         '{"ID":"2","Product1":"Butter","Type":"Grocery"}'
                                         ))
然而,这是不够的,因为它只给我第一行,我不能矢量化多行。更重要的是,它没有给我列名,因为输出如下所示:

V1 V2 V3 1面包汽车

有人能帮我写更好的代码或改进它,使之适用于多行吗。实际上,我必须运行数千个类似这样的json字符串。

librarytidyverse 图书馆在线 图书馆达夫 df% select-Product_JSON_字符串 >ID Product1类型 >面包店 >2 2黄油杂货店 >3.错误很严重 JSON_数据存储:3 x 3 >ID Product1类型 > >1面包汽车 >2 2黄油杂货店 >黄油杂货店 差额% 选择-Product_JSON_字符串, JSON_数据 分歧 >Daff比较:“df%>%select-Product_JSON_字符串”与“JSON_数据” >ID Product1类型 >->1家面包杂货店->汽车 >2黄油杂货店 >++3黄油杂货店 >-99错误很糟糕 daff::渲染差异 您可以在每个产品的JSON字符串上使用jsonlite::fromJSON并组合这些值

result <- do.call(rbind, lapply(df$Product_JSON_string, function(x) 
                  as.data.frame(jsonlite::fromJSON(x))))
result

#  ID Product1       Type
#1  1    Bread Automotive
#2  2   Butter    Grocery

只是在@ronak shah的答案中添加minify对我来说很有用


do.callbind_行,lapplydf$Product_json_字符串,functionx
as.data.framefromJSONminifyx

当我运行map_df时,我得到错误参数1必须命名。不确定这可能是哪个参数。我已经编辑了我的答案:map_df属于包purrr.do.callbind_rows,lapplydf$Product_json_string,functionx as.data.framefromJSONminifyx这适用于me@Hashir如果答案有帮助,请单击左侧的复选标记,随意接受答案。每个帖子只能接受一个答案。提及-
result <- do.call(rbind, lapply(df$Product_JSON_string, function(x) 
                  as.data.frame(jsonlite::fromJSON(x))))
result

#  ID Product1       Type
#1  1    Bread Automotive
#2  2   Butter    Grocery