【问题标题】:CSV to customized JSON format conversion in RCSV到R中的自定义JSON格式转换
【发布时间】:2016-02-19 10:46:11
【问题描述】:

我想将以下数据文件转换成 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

JSON格式应该是这样的

'[
    {
        "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"
                },

....等等

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

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

其他选项:

library(RJSONIO)

modified <- list(
  traits = colnames(data),
  Conditions = unname(apply(data, 1, function(x) as.data.frame(t(x))))
)
cat(toJSON(modified))

有什么建议吗?

【问题讨论】:

    标签: json r csv


    【解决方案1】:

    不要将其视为数据整理/修改操作,将其视为“模板”问题。这会遍历每个房子并填写一个 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"
            } 
          ] 
      }
    

    【讨论】:

    • 感谢您的帮助。还有一件事,在处理数据之后,我需要将文件发送到服务器,而不是在 R 控制台中打印。如何将结果保存到 json 文件并将其发送到服务器。我已经在 R 中编写并测试了将文件上传到服务器的命令,我现在唯一想要的就是保存 json 文件并上传。有什么想法吗?
    • 好吧,“如何以不同的方式解决这个问题”和“为我做我的工作”之间有一条界限。我认为这越界了。
    • 我写了以下代码作为你的扩展:` library(jsonlite) write(house_json, "test.json") json_file Error: parse error: invalid object key (must be a string) [{ , "id":"House_1", , "co (right here) ------^ 任何想法来解决这个问题,好吗?
    • 实际上,我对 R 有点陌生,并且努力学习它。不过,我很努力。对于上述问题,我写了几个小代码,但都没有工作。例如,作为你的house_json &lt;-(sprintf("[%s]", paste0(houses, collapse=",\n"))) write(house_json, "test.json") library(rjson) file &lt;- 'C:/Users/PMS458/Documents/test.json' con = file(file, "r") input &lt;- readLines(con, -1L) business.training &lt;- lapply(X=input,fromJSON) 的扩展@ Error: Error: parse error: premature EOF [{ (right here) ------^ Any Idea plz?
    • 感谢您亲爱的 hrbrmstr 的帮助。令人惊讶的是,在将输出文件发送到服务器时使用打印功能对我有用。再次感谢!
    猜你喜欢
    • 2021-10-18
    • 2012-06-08
    • 1970-01-01
    • 2013-06-14
    • 2013-04-22
    • 2023-04-08
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    相关资源
    最近更新 更多