简体   繁体   English

如何将列中带有列表的数据框转换为 R 中的 JSON?

[英]How to convert a data frame with a list in a column to a JSON in R?

I am trying to export a data frame which contains a list in a column, to a JSON and then save it in a format to share it.我正在尝试将包含列中列表的数据框导出到 JSON,然后以某种格式保存以共享它。

So far I am trying to use jsonlite to do the transformation with jsonlite::toJSON()到目前为止,我正在尝试使用jsonlitejsonlite::toJSON()

The data frame can be seen in the following image:数据框如下图所示:

DF

So far I have done this:到目前为止,我已经这样做了:

x <- jsonlite::toJSON(x = regiones, pretty = TRUE) # "regiones" is the data frame
cat(x)

And I get the following result.我得到以下结果。

I did not put all the values in the console output.我没有把所有的值都放在控制台输出中。

"codigo_region": "16",
    "geometry": {
      "type": "MultiPolygon",
      "coordinates": [
        [
          [
            [-72.3855, -36.9117],
            [-72.3895, -36.9162],
            [-72.3988, -36.9174],
            [-72.407, -36.9237],
            [-72.4077, -36.9285],
            [-72.419, -36.9305],
            [-72.4227, -36.926],

How can I save the object "x" which is a large json?如何保存大型 json 对象“x”?

Any other recommended approach?还有其他推荐的方法吗?

正如 Greg 和 Bruno 所说,使用write_json效果很好。

jsonlite::write_json(path = "path/to/save/your_file.json", x = regiones, pretty = TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM