结合 rawToChar() 和 write_json() 的问题

Problem combining rawToChar() and write_json()

我在使用 rawToChar() 和 write_json() 时遇到问题,我的结果是一个 .json 文件,其中包含 \n 和 \ 并且无法以“漂亮”的方式使用笔记本+。此外,我无法 展开展开 结果。

我的工作顺序是:

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)

当我用记事本+打开distribution.json时,我得到一行...

["{\n    \"ConfigurationStatus\": \"NOT_DEFINED\",\n    \"economicStatus ... blahblah \n}\n"]

有什么想法吗?

让一个文件example.json嘲笑你的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"]
        }
      }
    }
  }
}