我有rawToChar()和write_json()的问题,我的结果是一个.json文件,其中包括n和,不能在"pretty"中查看。使用笔记本+的方式。此外,我不能展开或展开结果。
我的工作顺序是:request <- httr::GET(
url = "https://myurl",
httr::content_type("application/json")
)
existing_distribution.json = jsonlite::prettify(rawToChar(request$content))
write_json(existing_distribution.json, "distribution.json"), pretty = TRUE)
当我打开分布。jsonwith notepad+我得到一行…
["{n "ConfigurationStatus": "NOT_DEFINED",n "economicStatus ... blahblah n}n"]
任何想法?
让有一个文件example.json
mock你的API包含
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
这是如何读取文件作为原始字节序列,解析它并将其保存为美化json:
library(jsonlite)
library(readr)
read_file_raw("example.json") %>%
rawToChar() %>%
parse_json() %>%
write_json("out.json", pretty = TRUE)
导致文件out.json
包含:
{
"glossary": {
"title": ["example glossary"],
"GlossDiv": {
"title": ["S"],
"GlossList": {
"GlossEntry": {
"ID": ["SGML"],
"SortAs": ["SGML"],
"GlossTerm": ["Standard Generalized Markup Language"],
"Acronym": ["SGML"],
"Abbrev": ["ISO 8879:1986"],
"GlossDef": {
"para": ["A meta-markup language, used to create markup languages such as DocBook."],
"GlossSeeAlso": [
["GML"],
["XML"]
]
},
"GlossSee": ["markup"]
}
}
}
}
}