【问题标题】:Creating JSON format data through data frames in R通过 R 中的数据框创建 JSON 格式数据
【发布时间】:2015-09-19 03:46:06
【问题描述】:

我想创建一个 JSON 格式的数据

{"Recipe Name": "ABC",
"Main Ingredient": "xyz",
"Ingredients": {type:"a"
                 id:"1"},
               {type:"b"
                 id:"2"},
                {type:"b"
                 id:"3"}
"Servings": 3,}

我有一个类型的数据框:

Recipe, Recipe Id ,Ingredients,Ingredients ID,Servings ,Main Ingredient,Main Ingredient ID  
 "abc"  , 2     ,    {"a","b","c"}  , {1,2,3,}   , 5   , "f"   ,7   
  "bcf"  , 3   ,       {"d","e","f"} , {4,5,7}  ,  4    ,"g"   ,8
   ....

我尝试了 usign rjson 包,但得到了 character(0) 作为输出。有人可以帮我解决这个问题吗?

我确实找到了一种方法

>library(RJSONIO)
>toJSON(dataframe)

这是输出:

[1] "{\n \"factor1\": [ \"115g\", \"1\", null, null ],\n\"unit1\": [ \"tub\", \ "cups\", \"希腊语\", \"baby\" ],\n\"item1\": [ \"tomatoes NA\", \"kalamata Olives\", \"feta cheese\", \"火箭 NA\" ] \n}"

不符合要求的格式

【问题讨论】:

  • 您的 data.frame 中是否确实存储了字符串 {"d","e","f"}?如果您提供 reproducible example (如果您的输入数据框可能会提供 dput() 以明确您正在使用的内容)会有所帮助。你尝试运行什么代码给你character(0)
  • R 数据框中的 Main Ingredient 替代品在哪里?
  • @MrFlick 是的,我的数据框中有这些刺痛。你能帮帮我吗?

标签: json r


【解决方案1】:

您想要的输出结构不正确:您缺少一些逗号(次要),并且您的Ingredients 需要是一个显式列表(使用[],不允许任何隐式)。此外,R 并不真正喜欢列名中的空格,所以我删除了它们。重组后的输出:

{"RecipeName": "ABC",
 "MainIngredient": "xyz",
 "Ingredients": [{"type":"a", "id":"1"},
                 {"type":"b", "id":"2"},
                 {"type":"b", "id":"3"}],
 "Servings": 3}

由于您的 data.frame 示例有点难以阅读/解析,我认为这是一个足够相似的示例:

dat <- data.frame(
    RecipeName=c('ABC', 'DEF'),
    MainIngredient=c('xyz', 'lmn'),
    Ingredients=c('{"a","b","c"}', '{"d","e","f"}'),
    IngredientsId=c('{1,2,3}', '{4,5,6}'),
    Servings=c(7,8),
    stringsAsFactors=FALSE
)
dat
##   RecipeName MainIngredient   Ingredients IngredientsId Servings
## 1        ABC            xyz {"a","b","c"}       {1,2,3}        7
## 2        DEF            lmn {"d","e","f"}       {4,5,6}        8

从这里开始,将Ingredients 中的嵌套列表转换为不仅仅是一个字符串需要一些努力:

datlist <- as.list(dat)
datlist$Ingredients <- mapply(function(a,b) {
    a1 <- strsplit(substring(a, 2, nchar(a)-1), ",")[[1]]
    ## might want to remove leading/trailing quotes as well
    a1 <- gsub('"', '', a1)
    b1 <- strsplit(substring(b, 2, nchar(b)-1), ",")[[1]]
    mapply(function(e,f) list(type=e, id=f), a1, b1, SIMPLIFY=FALSE, USE.NAMES=FALSE)
}, datlist$Ingredients, datlist$IngredientsId,
    SIMPLIFY=FALSE, USE.NAMES=FALSE)
datlist$IngredientsId <- NULL # to remove the now-unnecessary field

library(RJSONIO) # jsonlite::toJSON looks similar, not exactly the same
cat(toJSON(datlist, pretty=T))
## {
##  "RecipeName" : [
##      "ABC",
##      "DEF"
##  ],
##  "MainIngredient" : [
##      "xyz",
##      "lmn"
##  ],
##  "Ingredients" : [
##      [
##          {
##              "type" : "a",
##              "id" : "1"
##          },
##          {
##              "type" : "b",
##              "id" : "2"
##          },
##          {
##              "type" : "c",
##              "id" : "3"
##          }
##      ],
##      [
##          {
##              "type" : "d",
##              "id" : "4"
##          },
##          {
##              "type" : "e",
##              "id" : "5"
##          },
##          {
##              "type" : "f",
##              "id" : "6"
##          }
##      ]
##  ],
##  "Servings" : [
##      7,
##      8
##  ]
## }> 

不幸的是,从 data.frame 中取出一行会导致嵌套组件(特别是 Ingredients),因此它不能完全符合您想要的输出:

datlist2 <- as.list(dat[1,])
## rest of mapply(mapply(...)) ...
cat(toJSON(datlist2, pretty=TRUE))
## {
##  "RecipeName" : "ABC",
##  "MainIngredient" : "xyz",
##  "Ingredients" : [
##      [
##          {
##              "type" : "a",
##              "id" : "1"
##          },
##          {
##              "type" : "b",
##              "id" : "2"
##          },
##          {
##              "type" : "c",
##              "id" : "3"
##          }
##      ]
##  ],
##  "Servings" : 7
## }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多