如何将数据帧转换为自定义json格式

时间:2013-07-02 16:29:29

标签: json r

我需要将数据帧转换为R中的json fomrat。我的数据框y是这样的:

dput(y)
structure(list(Name = structure(c(38L, 23L, 16L, 35L, 21L, 6L, 
34L, 15L, 46L, 1L, 43L, 28L, 39L, 27L, 7L, 20L, 14L, 44L, 48L, 
36L), .Label = c("server09", "server10", "server11", "server12", 
"server13", "server14", "server15", "server16", "server17", "server18", 
"server19", "server20", "server21", "server22", "server23", "server24", 
"server25", "server26", "server27", "server28", "server29", "server30", 
"server31", "server32", "server33", "server34", "server35", "server36", 
"server37", "server38", "server39", "server40", "server41", "server42", 
"server43", "server44", "server45", "server46", "server47", "server48", 
"server49", "server50", "server51", "server52", "server53", "server54", 
"server55", "server56", "server57", "server58"), class = "factor"), 
    Date = c(1372737600, 1372737602, 1372737609, 1372737617, 
    1372737618, 1372737618, 1372737643, 1372737646, 1372737648, 
    1372737652, 1372737654, 1372737660, 1372737665, 1372737671, 
    1372737699, 1372737701, 1372737718, 1372737721, 1372737728, 
    1372737731), Cpu = c(3.9025, 36.3042, 2.6075, 3.1338, 0.9474, 
    0.149, 5.4401, 2.5652, 0.3612, 3.2651, 1.8703, 13.8967, 4.2438, 
    5.4401, 2.468, 0.9147, 1.4637, 7.2528, 6.119, 7.7009)), .Names = c("Name", 
"Date", "Cpu"), row.names = c(1L, 42L, 83L, 84L, 125L, 126L, 
127L, 168L, 169L, 202L, 203L, 236L, 277L, 318L, 359L, 360L, 361L, 
362L, 395L, 396L), class = "data.frame")

我需要我的json文件看起来像这样:

[{
    "name":'server13',
    "data": [ [1372737600,3.9025], [1372737602,10], [1372737609,10] ... [1372737731,20] ]
}, {
    "name":'server14',
    "data": [ [1372737600,4], [1372737602,10], [1372737609,10] ... [1372737731,30] ]
}]

我使用rjson包作为以下内容:

p <- toJSON(as.list(y))

我得到了这个输出:

"{\"Name\":[\"server13\",\"servar14\",...],\"Date\":[1372737600,1372737602,..],\"Cpu\":[3.9025,36.3042,..]}"

在R中有一种简单的方法吗?

3 个答案:

答案 0 :(得分:2)

您应该在

之前转换列表
library(rjson)
toJSON(unname(by(dat,dat$Name,
            FUN=function(x)
              list(Name=x[,1],data=unname(x[,-1])))))

[1] "[{\"Name\":\"server09\",\"data\":[1372737652,3.2651]},
      {\"Name\":\"server14\",\"data\":[1372737618,0.149]},
      {\"Name\":\"server15\",\"data\":[1372737699,2.468]}
      ........
      {\"Name\":\"server56\",\"data\":[1372737728,6.119]},null,null]"

<强> PS  请注意,这不适用于RJSONIO包,确实我收到此错误:

## Error: evaluation nested too deeply: infinite recursion /
## options(expressions=)?

答案 1 :(得分:1)

从data.frame的结构我猜这只是你数据的一小部分,服务器有多个条目:

# split by server
require(RJSONIO)
servers <- split(y, y$Name)
dumFun <- function(x){
  sData <- servers[x][[1]]
  if(nrow(sData) >0){
    # create appropriate list
    dumList <- unname(apply(sData[,2:3], 1, function(y) unname(as.list(y))))
    return(list(name = x, data = dumList)) 
  }
}

jsData <- lapply(names(servers), dumFun)
jsInd <- sapply(jsData, is.null)
toJSON(jsData[!jsInd])

写入文件时

  

[{ “名称”: “server09”, “数据”:[[1372737652,3.2651]]},{ “名称”: “server14”, “数据”:[[1372737618,0.149]]},{”命名 “:” server15" , “数据”:[[1372737699,2.468]]},{ “名称”: “server22”, “数据”:[[1372737718,1.4637]]},{ “名称”: “server23” “数据”:[[1372737646,2.5652]]},{ “名称”: “server24”, “数据”:[[1372737609,2.6075]]},{ “名称”: “server28”, “数据”:[ [1372737701,0.9147]]},{ “名称”: “server29”, “数据”:[[1372737618,0.9474]]},{ “名称”: “server31”, “数据”:[[1372737602,36.3042]] },{ “名称”: “server35”, “数据”:[[1372737671,5.4401]]},{ “名称”: “server36”, “数据”:[[1372737660,13.8967]]},{ “名” : “server42”, “数据”:[[1372737643,5.4401]]},{ “名称”: “server43”, “数据”:[[1372737617,3.1338]]},{ “名称”: “server44”,”数据 “:[[1372737731,7.7009]]},{” 名称 “:” server46" , “数据”:[[1372737600,3.9025]]},{ “名称”: “server47”, “数据”:[[1372737665 ,4.2438]]},{ “名称”: “server51”, “数据”:[[1372737654,1.8703]]},{ “名称”: “server52”, “数据”:[[1372737721,7.2528]]}, { “名称”: “server54”, “数据”:[[1372737648,0.3612]]},{ “名称”: “server56”, “数据”:[[1372737728,6.119]]}]

答案 2 :(得分:1)

我认为这种方法更简单

## take the first 3 observations
data <- head(y, 3)
data <- rbind(data,
           c(Name = "server24", Date = 1372737609, Cpu = 20.2345))

data
##        Name       Date     Cpu
## 1  server46 1372737600  3.9025
## 42 server31 1372737602 36.3042
## 83 server24 1372737609  2.6075
## 4  server24 1372737609 20.2345


## split the data
mylist <- split(data, data$Name)

## remove empty data.frame
mylist <- mylist[sapply(mylist, nrow) != 0]

## the ugliest part 
mylist <- unname(lapply(mylist, function(x)
                        list(name = unique(x[[1]]), 
                             data = matrix(x[2:3], nrow = 1))))

cat(RJSONIO::toJSON(mylist), "\n")


## [
##  {
##  "name": "server24",
## "data": [ [
##  [ "1372737609", "1372737609" ],
## [ "2.6075", "20.2345" ] 
## ] ] 
## },
## {
##  "name": "server31",
## "data": [ [
##  "1372737602",
## "36.3042" 
## ] ] 
## },
## {
##  "name": "server46",
## "data": [ [
##  "1372737600",
## "3.9025" 
## ] ] 
## } 
## ] 

<强> EDITED