如何将数据框转换为带有R中多列的json

时间:2013-07-18 14:31:48

标签: json r

我需要将数据帧转换为json格式,我的数据框如下所示:

dput(head(yyy,30))
structure(list(name = c("serverA", "serverA", "serverA", "serverA", 
"serverA", "serverA", "serverA", "serverA", "serverA", "serverA", 
"serverA", "serverA", "serverA", "serverA", "serverA", "serverA", 
"serverA", "serverA", "serverA", "serverA", "serverA", "serverA", 
"serverA", "serverA", "serverA", "serverA", "serverA", "serverA", 
"serverA", "serverA"), date = structure(c(1374120000, 1374120060, 
1374120120, 1374120180, 1374120360, 1374120420, 1374120540, 1374120600, 
1374120840, 1374120960, 1374121020, 1374121080, 1374121200, 1374121440, 
1374121500, 1374121620, 1374121680, 1374122040, 1374122160, 1374122280, 
1374122400, 1374122580, 1374122640, 1374122700, 1374122940, 1374123000, 
1374123120, 1374123180, 1374123240, 1374123360), class = c("POSIXct", 
"POSIXt"), tzone = "America/New_York"), resp = c(3644, 1067.5, 
2738, 5224, 561, 723, 522, 408.5, 446, 683.75, 521, 385, 2666.5, 
1268, 701, 143, 645, 474, 670.5, 549, 383, 1381, 483, 516, 467.5, 
10726, 931.5, 773, 778, 323), vol = c(1L, 2L, 1L, 1L, 1L, 1L, 
1L, 2L, 2L, 4L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L)), .Names = c("name", "date", 
"resp", "vol"), row.names = c(1L, 3L, 7L, 8L, 13L, 17L, 21L, 
25L, 33L, 39L, 42L, 44L, 48L, 59L, 63L, 68L, 71L, 83L, 88L, 91L, 
98L, 105L, 109L, 113L, 122L, 123L, 129L, 132L, 135L, 140L), class = "data.frame")

我的json需要像这样:

第一部分将包括日期和resp,第二部分将需要包括json格式的日期和卷。

[{"name":"serverA","yAxis":1, "data":[[<date>,<resp>],[<date>,<resp>]]},{"name":"serverA","yAxis":2, "data":[[<date>,<vol>],[<date>,<vol>]]}]

json out put需要像这样:

[{"name":"serverA","yAxis":1, "data":[[1374105840000,27.395],[1374107640000,26.646]]},{"name":"serverA","yAxis":2, "data":[[1374105840000,25.983],[1374107640000,22.724]]}]

如果数据框只包含resp,我可以做这样的转换:

servers <- split(yyy, yyy$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(toJSON(list(name = x, data = dumList))) 
  }
}


jsData <- lapply(names(servers), dumFun)
jsInd <- sapply(jsData, is.null)
p<-paste0('[', paste(jsData[!jsInd], collapse = ','), ']')

在R

中有一种简单的方法可以做到这一点

2 个答案:

答案 0 :(得分:0)

您的示例数据不包含您在输出中请求的某些变量,但我认为这应该对您有用。

library(rjson)

toJSON(data.frame(t(yyy)))

答案 1 :(得分:0)

我能够通过以下两个步骤完成此操作:

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(toJSON(list(name = x, yAxis=1, data = dumList))) 
          }
        }

        dumFun1 <- function(x){
          sData <- servers[x][[1]]

          if(nrow(sData) >0){
            # create appropriate list
            dumList <- unname(apply(sData[,c(2,4)], 1, function(y) unname(as.list(y))))
            return(toJSON(list(name = x, yAxis=2, data = dumList))) 
          }
        }

        jsData <- lapply(names(servers), dumFun)
        jsInd <- sapply(jsData, is.null)

        jsData1 <- lapply(names(servers), dumFun1)
        jsInd <- sapply(jsData, is.null)

        t<-paste(jsData, jsData1, sep=',')

        p<-paste0('[', paste(t[!jsInd], collapse = ','), ']')