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文件列表_Json_R_List - Fatal编程技术网

仅使用列表中的一个组件创建JSON文件列表

仅使用列表中的一个组件创建JSON文件列表,json,r,list,Json,R,List,我有4个json文件分布到文件夹中:folder1和folder2。每个json文件都包含日期、正文和标题 folder1.json: {"date": "December 31, 1989, Sunday, Late Edition - Final", "body": "Frigid temperatures across much of the United States this month sent demand for heating oil soaring, providing a

我有4个json文件分布到文件夹中:
folder1
folder2
。每个json文件都包含
日期
正文
标题

folder1.json:

{"date": "December 31, 1989, Sunday, Late Edition - Final", "body": "Frigid temperatures across much of the United States this month sent demand for heating oil soaring, providing a final upward jolt to crude oil prices. That's assuming the economy performs as expected - about 1 percent growth in G.N.P. The other big uncertainty is the U.S.S.R. If their production drops more than 4 percent, prices could stengthen. ", "title": "Prospects;"}

{"date": "December 31, 1989, Sunday, Late Edition - Final", "body": "DATELINE: WASHINGTON, Dec. 30 For years, experts have dubbed Czechoslovakia's spy agency the ''two Czech'' service. Agents of the Office for the Protection of State Secrets got one check from Prague, the pun goes, and another from their real bosses at K.G.B. headquarters in Moscow. Roy Godson, head of the Washington-based National Strategy Information Center and a well-known intelligence scholar, called any democratic change ''a net loss'' for Soviet intelligence. But he cautioned against euphoria. ''The Soviets wouldn't have relied on just official cooperation,'' he said. ''It would be surprising if they haven't unilaterally penetrated friendly services with their own agents, too.'' ", "title": "Upheaval in the East: Espionage;"}
folder2.json:

{"date": "December 31, 1989, Sunday, Late Edition - Final", "body": "SURVIVING the decline in the economy will be the overriding issue for 1990, say leaders of the county's business community.  But facing business owners are numerous problems, from taxes and regulations at all levels of government to competition from other businesses in and out of Westchester. Successful Westchester business owners will face and overcome these risks and obstacles. Westchester is a land of opportunity for the business owner. ", "title": "Coping With the Economic Prospects of 1990"}

{"date": "December 29, 1989, Friday, Late Edition - Final", "body": "Eastern Airlines said yesterday that it was laying off 600 employees, mostly managers, and cutting wages by 10 percent or 20 percent for about half its work force. Thomas J. Matthews, Eastern's senior vice president of human resources, estimated that the measures would save the carrier about $100 million a year. Eastern plans to rebuild by making Atlanta its primary hub and expects to operate about 75 percent of its flights from there. ", "title": "Eastern Plans Wage Cuts, 600 Layoffs"}
我想创建一个所有这些json文件的通用列表,但仅限于每篇文章的正文。到目前为止,我正在尝试以下方法:

json1 <- lapply(readLines("folder1.json"), fromJSON)
json2 <- lapply(readLines("folder2.json"), fromJSON)

jsonl <- list(json1$body, json2$body)
json1
require(RJSONIO)
json_1
require(RJSONIO)
安德烈斯·阿兹克塔

解决方案: 您需要在sapply()中撤销
fromJSON()
,以仅检索
正文

  • fromJSON()$body
注意:我假设文件格式是您提出的问题

文件格式的要点是sudojson,因此下面修改了
fromJSON()
调用

好的,让我们来看看一个例子:

阶段1:将JSON文件连接到1 输出
>文件列表newJSON newJSON
./data/NYT_1989.json                                                                                                                                        
[1,] 本月美国大部分地区寒冷的气温导致取暖用油需求飙升,最终导致原油价格上涨。一些现货原油的交易价格比一年前上涨了40%或更多。这些价格能维持吗?五位石油专家发表了他们的观点。这是假设经济表现如预期一样——国民生产总值增长约1%。另一个不确定因素是苏联。如果他们的产量下降超过4%,价格可能会进一步上涨。"
[2,]”日期:华盛顿12月30日多年来,专家们将捷克斯洛伐克的间谍机构称为“两捷克”服务机构。但他告诫人们不要过度兴奋“苏联不会仅仅依靠官方合作,”他说如果他们没有单方面地与自己的代理人一起渗透友好服务,那将是令人惊讶的。”
[3]“该县商界领袖说,在经济衰退中生存将是1990年的首要问题。成功的Westchester企业主将面临并克服这些风险和障碍。韦斯特切斯特对于企业主来说是一块充满机遇的土地。"                                                                                                                                                                                                                  
./data/NYT_1990.json                                                                                                                                       
[1]“本月,美国大部分地区的气温呈蓝色,导致取暖用油需求飙升,最终导致原油价格上扬。一些现货原油的交易价格比一年前上涨了40%或更多。这些价格能维持吗?五位石油专家发表了他们的观点。这是假设经济表现如预期一样——国民生产总值增长约1%。另一个不确定因素是苏联。如果他们的产量下降超过4%,价格可能会进一步上涨。"
[2,]蓝色1:华盛顿,12月30日多年来,专家们将捷克斯洛伐克的间谍机构称为“两捷克”服务机构。但他告诫人们不要过度兴奋“苏联不会仅仅依靠官方合作,”他说如果他们没有单方面地与自己的代理人一起渗透友好服务,那将是令人惊讶的。”
[3,]绿色4该县商界领袖表示,经济下滑将是1990年的首要问题。成功的Westchester企业主将面临并克服这些风险和障碍。韦斯特切斯特对于企业主来说是一块充满机遇的土地。" 
Andres Azqueta

解决方案: 您需要在sapply()中撤销
fromJSON()
,以仅检索
正文

  • fromJSON()$body
注意:我假设文件格式是您提出的问题

文件格式的要点是sudojson,因此下面修改了
fromJSON()
调用

好的,让我们来看看一个例子:

阶段1:将JSON文件连接到1 输出
>文件列表newJSON newJSON
./data/NYT_1989.json                                                                                                                                         
[1]“本月美国大部分地区寒冷的气温导致取暖用油需求飙升,最终导致原油价格上涨。一些现货原油的交易价格比一年前上涨了40%或更多。威尔蒂斯
require(RJSONIO)   
json_1<- fromJSON("~/folder1/1.json")
json_2<- fromJSON("~/folder2/2.json")

jsonl <- list(json1$body, json2$body)
filelist <- c("./data/NYT_1989.json", "./data/NYT_1990.json")
newJSON <- sapply(filelist, function(x) fromJSON(sprintf("[%s]", paste(readLines(x), collapse = ",")), flatten = FALSE))
newJSON[2]# Extract bodies
newJSON[5]# Extract bodies
filelist <- c("./data/NYT_1989.json", "./data/NYT_1990.json")
> newJSON <- sapply(filelist, function(x) fromJSON(sprintf("[%s]", paste(readLines(x), collapse = ",")), flatten = FALSE))
> newJSON[2]# Extract bodies
[[1]]
[1] "Frigid temperatures across much of the United States this month sent demand for heating oil soaring, providing a final upward jolt to crude oil prices. Some spot crude traded at prices up 40 percent or more from a year ago. Will these prices hold? Five experts on oil offer their views. That's assuming the economy performs as expected - about 1 percent growth in G.N.P. The other big uncertainty is the U.S.S.R. If their production drops more than 4 percent, prices could stengthen. "
[2] "DATELINE: WASHINGTON, Dec. 30 For years, experts have dubbed Czechoslovakia's spy agency the ''two Czech'' service. But he cautioned against euphoria. ''The Soviets wouldn't have relied on just official cooperation,'' he said. ''It would be surprising if they haven't unilaterally penetrated friendly services with their own agents, too.'' "                                                                                                                                                
[3] "SURVIVING the decline in the economy will be the overriding issue for 1990, say leaders of the county's business community. Successful Westchester business owners will face and overcome these risks and obstacles. Westchester is a land of opportunity for the business owner. "                                                                                                                                                                                                                  

> newJSON[5]# Extract bodies
[[1]]
[1] "Blue temperatures across much of the United States this month sent demand for heating oil soaring, providing a final upward jolt to crude oil prices. Some spot crude traded at prices up 40 percent or more from a year ago. Will these prices hold? Five experts on oil offer their views. That's assuming the economy performs as expected - about 1 percent growth in G.N.P. The other big uncertainty is the U.S.S.R. If their production drops more than 4 percent, prices could stengthen. "
[2] "BLUE1: WASHINGTON, Dec. 30 For years, experts have dubbed Czechoslovakia's spy agency the ''two Czech'' service. But he cautioned against euphoria. ''The Soviets wouldn't have relied on just official cooperation,'' he said. ''It would be surprising if they haven't unilaterally penetrated friendly services with their own agents, too.'' "                                                                                                                                                 
[3] "GREEN4 the decline in the economy will be the overriding issue for 1990, say leaders of the county's business community. Successful Westchester business owners will face and overcome these risks and obstacles. Westchester is a land of opportunity for the business owner. "   
filelist <- c("./data/NYT_1989.json", "./data/NYT_1990.json")
newJSON <- sapply(filelist, function(x) fromJSON(sprintf("[%s]", paste(readLines(x), collapse = ",")), flatten = FALSE)$body)
newJSON
> filelist <- c("./data/NYT_1989.json", "./data/NYT_1990.json")
> newJSON <- sapply(filelist, function(x) fromJSON(sprintf("[%s]", paste(readLines(x), collapse = ",")), flatten = FALSE)$body)
> newJSON
     ./data/NYT_1989.json                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
[1,] "Frigid temperatures across much of the United States this month sent demand for heating oil soaring, providing a final upward jolt to crude oil prices. Some spot crude traded at prices up 40 percent or more from a year ago. Will these prices hold? Five experts on oil offer their views. That's assuming the economy performs as expected - about 1 percent growth in G.N.P. The other big uncertainty is the U.S.S.R. If their production drops more than 4 percent, prices could stengthen. "
[2,] "DATELINE: WASHINGTON, Dec. 30 For years, experts have dubbed Czechoslovakia's spy agency the ''two Czech'' service. But he cautioned against euphoria. ''The Soviets wouldn't have relied on just official cooperation,'' he said. ''It would be surprising if they haven't unilaterally penetrated friendly services with their own agents, too.'' "                                                                                                                                                
[3,] "SURVIVING the decline in the economy will be the overriding issue for 1990, say leaders of the county's business community. Successful Westchester business owners will face and overcome these risks and obstacles. Westchester is a land of opportunity for the business owner. "                                                                                                                                                                                                                  
     ./data/NYT_1990.json                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
[1,] "Blue temperatures across much of the United States this month sent demand for heating oil soaring, providing a final upward jolt to crude oil prices. Some spot crude traded at prices up 40 percent or more from a year ago. Will these prices hold? Five experts on oil offer their views. That's assuming the economy performs as expected - about 1 percent growth in G.N.P. The other big uncertainty is the U.S.S.R. If their production drops more than 4 percent, prices could stengthen. "
[2,] "BLUE1: WASHINGTON, Dec. 30 For years, experts have dubbed Czechoslovakia's spy agency the ''two Czech'' service. But he cautioned against euphoria. ''The Soviets wouldn't have relied on just official cooperation,'' he said. ''It would be surprising if they haven't unilaterally penetrated friendly services with their own agents, too.'' "                                                                                                                                                 
[3,] "GREEN4 the decline in the economy will be the overriding issue for 1990, say leaders of the county's business community. Successful Westchester business owners will face and overcome these risks and obstacles. Westchester is a land of opportunity for the business owner. "