将JSON字符串解析为R列表

时间:2016-12-11 13:15:59

标签: json r jsonlite

headOption您好我有我的JSON文件,我想在列表中解析它。我想知道我是否只能解析其中包含特定字符串的行?例如

enter code here

我只想解析{"app":15c,"device_carrier":"Verizon Wireless"} {"app":15b,"device_carrier":"Verizon Wireless"} {"app":15a,"device_carrier":"Verizon Wireless"} 行 我希望有一个列表与最后一行!(问题是我有很多数据,我的计算机无法解析整个JSON文件,我只需要一个应用程序)

1 个答案:

答案 0 :(得分:0)

有一些方法可以优化以下内容,但在您的情况下可能没有必要:

library(jsonlite)
library(dplyr)

df <- data_frame()
jsonlite::stream_in(file("/tmp/apps.json"), function(x) { df <<- bind_rows(df, filter(x, app=="15b")) })

我制作了一个看起来像的文件:

{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}

运行该代码,您将获得:

df
## # A tibble: 13 × 2
##      app   device_carrier
##    <chr>            <chr>
## 1    15b Verizon Wireless
## 2    15b Verizon Wireless
## 3    15b Verizon Wireless
## 4    15b Verizon Wireless
## 5    15b Verizon Wireless
## 6    15b Verizon Wireless
## 7    15b Verizon Wireless
## 8    15b Verizon Wireless
## 9    15b Verizon Wireless
## 10   15b Verizon Wireless
## 11   15b Verizon Wireless
## 12   15b Verizon Wireless
## 13   15b Verizon Wireless