我正在尝试将多个json文件读取到工作目录中,以进一步转换为数据集。我在目录json中有文件text1,text2,text3。这是我写的代码:
setwd("Users/Desktop/json")
temp = list.files(pattern="text*.")
myfiles = lapply(temp, read.delim)
library("rjson")
json_file <- "myfiles"
library(jsonlite)
out <- jsonlite::fromJSON(json_file)
out[vapply(out, is.null, logical(1))] <- "none"
data.frame(out, stringsAsFactors = FALSE)[,1:5]
View(out)
Run Code Online (Sandbox Code Playgroud)
我大约有200个文件,所以我想知道是否可以导入json文件。
谢谢
这是 MWE,如何获得正确的数字作为字符。
require(jsonlite)
j <- "{\"id\": 323907258301939713}"
a <- fromJSON(j)
print(a$id, digits = 20)
class(a$id)
a$id <- as.character(a$id)
a$id
class(a$id)
Run Code Online (Sandbox Code Playgroud)
这是输出。
Loading required package: jsonlite
Loading required package: methods
[1] 323907258301939712
[1] "numeric"
[1] "323907258301939712"
[1] "character"
Run Code Online (Sandbox Code Playgroud)
我想获得确切的数字323907258301939713作为字符a
我正在尝试将大型 JSONL(.gz) 文件拆分为多个 .csv 文件。我已经能够使用下面的代码为前 25.000 个条目创建一个有效的 .csv 文件。我现在想读取和解析 25.001 到第 50.000 行,但一直无法这样做。我觉得这应该很容易完成,但到目前为止我的搜索没有结果。
有没有办法操纵 readLines 函数中的“n”因子来选择特定范围的行?
(ps我正在学习;))
setwd("filename")
a<-list.files(pattern="(.*?).0.jsonl.gz")
a[1]
raw.data<- readLines(gzfile(a[1]), warn = "T",n=25000)
rd <- fromJSON(paste("[",paste(raw.data,collapse=','),']'))
rd2<-do.call("cbind", rd)
file=paste0(a,".csv.gz")
write.csv.gz(rd2, file, na="", row.names=FALSE)
Run Code Online (Sandbox Code Playgroud) 我正在尝试将以下JSON文件("my_file.json")读入R,其中包含以下内容:
[{"id":"484","comment":"They call me "Bruce""}]
Run Code Online (Sandbox Code Playgroud)
使用jsonlite包(0.9.12),以下操作失败:
library(jsonlite)
fromJSON(readLines('~/my_file.json'))
Run Code Online (Sandbox Code Playgroud)
收到错误:
"Error in parseJSON(txt) : lexical error: invalid char in json text.
84","comment":"They call me "Bruce""}]
(right here) ------^"
Run Code Online (Sandbox Code Playgroud)
这是R转义文件的输出:
readLines('~/my_file.json')
"[{\"id\":\"484\",\"comment\":\"They call me \"Bruce\"\"}]"
Run Code Online (Sandbox Code Playgroud)
删除"Bruce"周围的引号可以解决问题,如:
my_file.json
[{"id":"484","comment":"They call me Bruce"}]
Run Code Online (Sandbox Code Playgroud)
但是擒纵机构的问题是什么?
我正在尝试fromJSON()读取具有多个对象的 .json 文件,其结构如下:
{ "key11": value11, "key12": value12 }\n{ "key11": value11, "key12": value12 }\n\xe2\x80\xa6\nRun Code Online (Sandbox Code Playgroud)\n\n如果我手动[...]在整个文件周围添加括号,并,在对象之间添加逗号,则以下代码有效:
json_file <- "file.json"\njson_data <- fromJSON(json_file,flatten=TRUE)\nRun Code Online (Sandbox Code Playgroud)\n\n但是添加括号和逗号对于我实际尝试做的事情来说是不可行的(我在示例文件上做到了)。
\n\n我尝试使用readLines(),因为对象之间有换行符,但我没有得到任何结果。
如何在不修改文件的情况下读取这些 json 对象?
\n我有一段时间试图将JSON文件转换为数据帧.我已经搜索过并试图将其他人的代码用于我的示例,但似乎都没有.输出始终是列表而不是数据帧.
library(jsonlite)
URL <- getURL("http://scores.nbcsports.msnbc.com/ticker/data/gamesMSNBC.js.asp?xml=true&sport=NBA&period=20160104")
URLP <- fromJSON(URL, simplifyDataFrame = TRUE, flatten = FALSE)
URLP
Run Code Online (Sandbox Code Playgroud)
这是答案总是以什么格式结束.
$games
[1] "<ticker-entry gamecode=\"2016010405\" gametype=\"Regular Season\"><visiting-team display_name=\"Toronto\" alias=\"Tor\" nickname=\"Raptors\" id=\"28\" division=\"ECA\" conference=\"EC\" score=\"\"><score heading=\"\" value=\"0\" team-fouls=\"0\"></score><team-record wins=\"21\" losses=\"14\"></team-record><team-logo link=\"http://hosted.stats.com/nba/logos/nba_50x33/Toronto_Raptors.png\" gz-image=\"http://hosted.stats.com/GZ/images/NBAlogos/TorontoRaptors.png\"></team-logo></visiting-team><home-team display_name=\"Cleveland\" alias=\"Cle\" nickname=\"Cavaliers\" id=\"5\" division=\"ECC\" conference=\"EC\" score=\"\"><score heading=\"\" value=\"0\" team-fouls=\"0\"></score><team-record wins=\"22\" losses=\"9\" ties=\"\"></team-record><team-logo link=\"http://hosted.stats.com/nba/logos/nba_50x33/Cleveland_Cavaliers.png\" gz-image=\"http://hosted.stats.com/GZ/images/NBAlogos/ClevelandCavaliers.png\"></team-logo></home-team><gamestate status=\"Pre-Game\" display_status1=\"7:00 PM\" display_status2=\"\" href=\"http://scores.nbcsports.msnbc.com/nba/preview.asp?g=2016010405\" tv=\"FSOH/SNT\" gametime=\"7:00 PM\" gamedate=\"1/4\" is-dst=\"0\" is-world-dst=\"0\"></gamestate></ticker-entry>"
Run Code Online (Sandbox Code Playgroud) 我有以下R数据框:
Values
Type1 123
Type2 4565
Type3 7812
Run Code Online (Sandbox Code Playgroud)
我希望JSON输出是
{"Type1":123, "Type2":4565, "Type3":7812}
Run Code Online (Sandbox Code Playgroud)
数字字段可以加引号
我使用jsonlite toJSON,输出为:
[{"Values":123,"_row":"Type1"},
{"Values": 4565,"_row":"Type2"},
{"Values": 7812,"_row":"Type3"}]
Run Code Online (Sandbox Code Playgroud) 使用以下命令可以读取 json 文件:
library(jsonlite)
json_text <- readLines("tect.json", warn = FALSE, encoding = "UTF-8")
Run Code Online (Sandbox Code Playgroud)
json_data <- fromJSON(txt = paste(json_text, collapse = ""))
Run Code Online (Sandbox Code Playgroud)
tect.json 文件中的数据格式如下:
[
{
"id": 1,
"name": "Apple",
"color": "red",
"price": 0.99
},
{
"id": 2,
"name": "Banana",
"color": "yellow",
"price": 0.5
},
{
"id": 3,
"name": "Orange",
"color": "orange",
"price": 0.75
}
]
Run Code Online (Sandbox Code Playgroud)
但是,如果 tech.json 具有这种格式,如何将其作为 json 文件读取并将其转换为数据帧?
{"id": 1, "name": "Apple", "color": "red", "price": 0.99 }
{"id": 2, "name": "Banana", "color": "yellow", "price": 0.5 …Run Code Online (Sandbox Code Playgroud) fromJSON我正在使用包中的JSON 将 JSON 转换为 R 对象jsonlite,但数值正在四舍五入。如何控制转换后数值的精度?
例子
library(jsonlite)
fromJSON('{"lon": -86.143278324353244}')
## $lon
## [1] -86.14328
## Desired output is -86.143278324353244
Run Code Online (Sandbox Code Playgroud)
查看代码jsonlite,我将函数调用追溯到parse.c中的 R_parse
.Call(jsonlite:::R_parse, "-86.143278324353244", FALSE)
## [1] -86.14328
Run Code Online (Sandbox Code Playgroud)
其他尝试
fromJSON有一个digits参数,但它不是 的有效参数toJSON。rjson和RJSONIO产生类似的结果。我需要使用以下格式的水暖工包从R发送响应
{
"status": "SUCCESS",
"code": "200",
"output": {
"studentid": "1001",
"name": "Kevin"
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到以下格式
[
"{\n \"status\": \"SUCCESS\",\n \"code\": \"200\",\n \"output\": {\n \"studentid\": \"1001\",\n \"name\": \"Kevin\"\n }\n}"
]
Run Code Online (Sandbox Code Playgroud)
请帮助我正确格式化此json
我的密码
#* @post /sum
addTwo <- function(){
library(jsonlite)
x <- list(status = "SUCCESS", code = "200",output = list(studentid = "1001", name = "Kevin"))
output<-toJSON(x,pretty = TRUE, auto_unbox = TRUE)
return (output)
}
Run Code Online (Sandbox Code Playgroud)