用于在R中自定义JSON格式转换的CSV

时间:2016-02-19 10:46:12

标签: json r csv

我想将以下数据文件转换为JSON格式

\([^()]*(?:\b\([^()]*\)[^()]*)*\)

JSON格式应该是这样的

House   space   type        ID  less than 18    18 to 23    Greather than 23
1   Livingroom  Temperature 1   0             29.44004742   70.55995258
1   Hallway     temperature 1   14.59211237   61.59805511   23.80983252
1   Bedroom     temperature 1   1.683093749   60.63394348   37.68296277
2   Livingroom  Temperature 2   17.16494111   49.53457447   33.30048442
2   Hallway     temperature 2   36.3833926    49.56992189   14.04668551
2   Bedroom     temperature 2   39.74861892   53.78744108   6.463939993

....等等

我尝试了不同的选项,重塑,jsonlite,RJSONIO但无法获得所需的结果。 例如

'[
    {
        "id":"house_1", 
        "condition":
            [
                {
                    "type":"temperature",
                    "segment":{"Less than 18":20, "18-25":30 , "Greater than 25":10},
                    "unit":"day",
                    "space":"livingroom"
                },
                {
                    "type":"temperature",
                    "segment":{"Less Lan 18":20, "18-25":30 , "Greater than 25":10},
                    "unit":"day",
                    "space":"hallway"
                },
                {
                    "type":"temperature",
                    "segment":{"Less Lan 18":20, "18-25":30 , "Greater than 25":10},
                    "unit":"day",
                    "space":"bedroom"
                },

]
    },

{
        "id":"house_2", 
        "condition":
            [
                {
                    "type":"temperature",
                    "segment":{"Less Lan 18":20, "18-25":30 , "Greater than 25":10},
                    "unit":"day"
                },

其他选择:

library(rjson)
library(reshape2)

mm <- melt(newdata)

y <- melt(data, id.vars=c("ID", "type", "space"), measure.vars=c("less.than.18", "X18.to.23", "Greather.than.23"), variable.name="Segment",
          value.name="percentage")

ss <- split(y, y$ID)
exportJson <- toJSON(ss)
exportJson

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

请不要将其视为数据整理/修改操作,将其视为&#34;模板&#34;问题。这将迭代每个房屋并填写JSON模板字符串。

注意:我故意不使用外部数组括号,因为这应该是鼓励阅读/理解代码而不是剪切/粘贴此答案。

dat <- read.table(text="House   space   type        ID  less.than.18    18.to.23    Greather.than.23
1   Livingroom  Temperature 1   0             29.44004742   70.55995258
1   Hallway     temperature 1   14.59211237   61.59805511   23.80983252
1   Bedroom     temperature 1   1.683093749   60.63394348   37.68296277
2   Livingroom  Temperature 2   17.16494111   49.53457447   33.30048442
2   Hallway     temperature 2   36.3833926    49.56992189   14.04668551
2   Bedroom     temperature 2   39.74861892   53.78744108   6.463939993",
                  header=TRUE)

by(dat, list(dat$House), function(x) {

  outer_template <- '  { 
    "id":"House_%s", 
    "condition":
      [ 
%s 
      ] 
  }'

  inner_template <- '        { "type":"%s", 
          "segment":{"Less than 18":%s, "18-25":%s , "Greater than 25":%s},
          "unit":"day",
          "space":"%s"
        }'

  condition <- paste0(apply(x, 1, function(y) {
    sprintf(inner_template, 
            tolower(y["type"]), y["less.than.18"], y["X18.to.23"], 
            y["Greather.than.23"], tolower(y["space"]))
  }), collapse=",\n")

  sprintf(outer_template, x$House[1], condition)

}) -> houses

house_json <- paste0(houses, collapse=",\n")
cat(house_json)

  { 
    "id":"House_1", 
    "condition":
      [ 
        { "type":"temperature", 
          "segment":{"Less than 18": 0.000000, "18-25":29.44005 , "Greater than 25":70.55995},
          "unit":"day",
          "space":"livingroom"
        },
        { "type":"temperature", 
          "segment":{"Less than 18":14.592112, "18-25":61.59806 , "Greater than 25":23.80983},
          "unit":"day",
          "space":"hallway"
        },
        { "type":"temperature", 
          "segment":{"Less than 18": 1.683094, "18-25":60.63394 , "Greater than 25":37.68296},
          "unit":"day",
          "space":"bedroom"
        } 
      ] 
  },
  { 
    "id":"House_2", 
    "condition":
      [ 
        { "type":"temperature", 
          "segment":{"Less than 18":17.16494, "18-25":49.53457 , "Greater than 25":33.30048},
          "unit":"day",
          "space":"livingroom"
        },
        { "type":"temperature", 
          "segment":{"Less than 18":36.38339, "18-25":49.56992 , "Greater than 25":14.04669},
          "unit":"day",
          "space":"hallway"
        },
        { "type":"temperature", 
          "segment":{"Less than 18":39.74862, "18-25":53.78744 , "Greater than 25": 6.46394},
          "unit":"day",
          "space":"bedroom"
        } 
      ] 
  }