1

Rには次のような関数があります:

output<-function(myname){
  library(rjson)
  #setwd("C:/Rstudio/vCenter")
  ##vcenter <- system.file("instvcenter.csv", package = packageName())
  #res <- read.csv("vcenter.csv", header = TRUE, sep = ",")

  res<-structure(list(Host = 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", "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", "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", "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", "ServerA", "ServerA", "ServerA", "ServerA", 
                          "ServerA", "ServerA", "ServerA", "ServerA", "ServerA", "ServerA", 
                          "ServerA", "ServerA", "ServerA", "ServerA", "ServerA", "ServerA", 
                          "ServerA"), Time = c(1406284200L, 1406286000L, 1406287800L, 1406289600L, 
                                               1406291400L, 1406293200L, 1406295000L, 1406296800L, 1406298600L, 
                                               1406300400L, 1406302200L, 1406354400L, 1406356200L, 1406358000L, 
                                               1406359800L, 1406361600L, 1406363400L, 1406365200L, 1406367000L, 
                                               1406370600L, 1406372400L, 1406374200L, 1406376000L, 1406379600L, 
                                               1406381400L, 1406383200L, 1406385000L, 1406388600L, 1406390400L, 
                                               1406392200L, 1406394000L, 1406397600L, 1406399400L, 1406401200L, 
                                               1406403000L, 1406406600L, 1406408400L, 1406489400L, 1406491200L, 
                                               1406493000L, 1406494800L, 1406496600L, 1406498400L, 1406500200L, 
                                               1406502000L, 1406503800L, 1406505600L, 1406507400L, 1406509200L, 
                                               1406511000L, 1406512800L, 1406514600L, 1406516400L, 1406518200L, 
                                               1406520000L, 1406521800L, 1406523600L, 1406525400L, 1406527200L, 
                                               1406529000L, 1406530800L, 1406532600L, 1406534400L, 1406536200L, 
                                               1406538000L, 1406539800L, 1406541600L, 1406543400L, 1406545200L, 
                                               1406547000L, 1406548800L, 1406550600L, 1406552400L, 1406554200L, 
                                               1406556000L, 1406557800L, 1406561400L, 1406563200L, 1406565000L, 
                                               1406566800L, 1406568600L, 1406570400L, 1406572200L, 1406574000L, 
                                               1406575800L, 1406577600L, 1406579400L, 1406581200L, 1406583000L, 
                                               1406584800L, 1406586600L, 1406588400L, 1406590200L, 1406592000L, 
                                               1406593800L, 1406595600L, 1406597400L, 1406599200L, 1406601000L, 
                                               1406602800L, 1406604600L, 1406606400L, 1406608200L, 1406610000L, 
                                               1406611800L, 1406613600L, 1406615400L, 1406617200L, 1406619000L, 
                                               1406620800L, 1406622600L, 1406624400L, 1406626200L, 1406628000L, 
                                               1406629800L, 1406631600L, 1406633400L, 1406635200L, 1406637000L, 
                                               1406638800L, 1406640600L, 1406642400L, 1406644200L, 1406646000L, 
                                               1406647800L, 1406649600L, 1406651400L, 1406653200L, 1406655000L, 
                                               1406656800L, 1406658600L, 1406660400L, 1406662200L, 1406664000L, 
                                               1406665800L, 1406667600L, 1406669400L), Cpu = c(0.091999998, 
                                                                                               0.091999998, 0.091199999, 0.092799997, 0.091199999, 0.0904, 0.092799997, 
                                                                                               0.093599997, 0.091999998, 0.0904, 0.093599997, 0.0096, 0.0094, 
                                                                                               0.0094, 0.0094, 0.0092, 0.0094, 0.0092, 0.0094, 0.0096, 0.0094, 
                                                                                               0.0092, 0.0092, 0.0094, 0.0092, 0.0094, 0.0092, 0.0094, 0.0094, 
                                                                                               0.0096, 0.0092, 0.0092, 0.0094, 0.0092, 0.0092, 0.0094, 0.0092, 
                                                                                               0.0094, 0.0094, 0.0092, 0.0094, 0.0094, 0.0092, 0.0094, 0.0092, 
                                                                                               0.0094, 0.0112, 0.0096, 0.0094, 0.0094, 0.0094, 0.0096, 0.0094, 
                                                                                               0.0092, 0.0092, 0.0094, 0.0094, 0.0092, 0.0092, 0.0092, 0.0092, 
                                                                                               0.0092, 0.0092, 0.0094, 0.0092, 0.0094, 0.0092, 0.0098, 0.0092, 
                                                                                               0.0092, 0.0094, 0.0094, 0.0092, 0.0092, 0.0094, 0.0094, 0.0094, 
                                                                                               0.0094, 0.0094, 0.0094, 0.0092, 0.0092, 0.0092, 0.0092, 0.0092, 
                                                                                               0.0092, 0.0094, 0.0094, 0.0092, 0.0092, 0.0092, 0.0092, 0.0092, 
                                                                                               0.0094, 0.0092, 0.0094, 0.0094, 0.0092, 0.0094, 0.0092, 0.0092, 
                                                                                               0.0092, 0.0092, 0.0092, 0.0092, 0.0092, 0.0092, 0.0092, 0.0096, 
                                                                                               0.0092, 0.0092, 0.0094, 0.0094, 0.0094, 0.0096, 0.0114, 0.012, 
                                                                                               0.011799999, 0.012, 0.012, 0.011799999, 0.0116, 0.012, 0.011799999, 
                                                                                               0.012, 0.011799999, 0.011, 0.0116, 0.0116, 0.011799999, 0.011799999, 
                                                                                               0.012, 0.013, 0.011799999, 0.011799999, 0.011799999, 0.011799999
                                               )), .Names = c("Host", "Time", "Cpu"), row.names = c(NA, -137L
                                               ), class = "data.frame")

res$Cpu<-as.numeric(round(res$Cpu, digits=3))
res$Time<-as.numeric(res$Time*1000-14400)

servers <- split(res, res$Host)
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<-paste(jsData[!jsInd], collapse = ',')
#list(
#p<-paste('[', p, ']')
#)
return(p)

}

この関数をajax呼び出しで呼び出すと、これが出力になります。名前、ServerA、データの前に「\」を付けます。このコードを変更して、それを行わないようにするにはどうすればよいですか? 何か案は?

[
    "{\"name\":\"ServerA\",\"data\":[[1406284185600,0.092],[1406285985600,0.092],[1406287785600,0.091],[1406289585600,0.093],[1406291385600,0.091],[1406293185600,0.09],[1406294985600,0.093],[1406296785600,0.094],[1406298585600,0.092],[1406300385600,0.09],[1406302185600,0.094],[1406354385600,0.01],[1406356185600,0.009],[1406357985600,0.009],[1406359785600,0.009],[1406361585600,0.009],[1406363385600,0.009],[1406365185600,0.009],[1406366985600,0.009],[1406370585600,0.01],[1406372385600,0.009],[1406374185600,0.009],[1406375985600,0.009],[1406379585600,0.009],[1406381385600,0.009],[1406383185600,0.009],[1406384985600,0.009],[1406388585600,0.009],[1406390385600,0.009],[1406392185600,0.01],[1406393985600,0.009],[1406397585600,0.009],[1406399385600,0.009],[1406401185600,0.009],[1406402985600,0.009],[1406406585600,0.009],[1406408385600,0.009],[1406489385600,0.009],[1406491185600,0.009],[1406492985600,0.009],[1406494785600,0.009],[1406496585600,0.009],[1406498385600,0.009],[1406500185600,0.009],[1406501985600,0.009],[1406503785600,0.009],[1406505585600,0.011],[1406507385600,0.01],[1406509185600,0.009],[1406510985600,0.009],[1406512785600,0.009],[1406514585600,0.01],[1406516385600,0.009],[1406518185600,0.009],[1406519985600,0.009],[1406521785600,0.009],[1406523585600,0.009],[1406525385600,0.009],[1406527185600,0.009],[1406528985600,0.009],[1406530785600,0.009],[1406532585600,0.009],[1406534385600,0.009],[1406536185600,0.009],[1406537985600,0.009],[1406539785600,0.009],[1406541585600,0.009],[1406543385600,0.01],[1406545185600,0.009],[1406546985600,0.009],[1406548785600,0.009],[1406550585600,0.009],[1406552385600,0.009],[1406554185600,0.009],[1406555985600,0.009],[1406557785600,0.009],[1406561385600,0.009],[1406563185600,0.009],[1406564985600,0.009],[1406566785600,0.009],[1406568585600,0.009],[1406570385600,0.009],[1406572185600,0.009],[1406573985600,0.009],[1406575785600,0.009],[1406577585600,0.009],[1406579385600,0.009],[1406581185600,0.009],[1406582985600,0.009],[1406584785600,0.009],[1406586585600,0.009],[1406588385600,0.009],[1406590185600,0.009],[1406591985600,0.009],[1406593785600,0.009],[1406595585600,0.009],[1406597385600,0.009],[1406599185600,0.009],[1406600985600,0.009],[1406602785600,0.009],[1406604585600,0.009],[1406606385600,0.009],[1406608185600,0.009],[1406609985600,0.009],[1406611785600,0.009],[1406613585600,0.009],[1406615385600,0.009],[1406617185600,0.009],[1406618985600,0.01],[1406620785600,0.009],[1406622585600,0.009],[1406624385600,0.009],[1406626185600,0.009],[1406627985600,0.009],[1406629785600,0.01],[1406631585600,0.011],[1406633385600,0.012],[1406635185600,0.012],[1406636985600,0.012],[1406638785600,0.012],[1406640585600,0.012],[1406642385600,0.012],[1406644185600,0.012],[1406645985600,0.012],[1406647785600,0.012],[1406649585600,0.012],[1406651385600,0.011],[1406653185600,0.012],[1406654985600,0.012],[1406656785600,0.012],[1406658585600,0.012],[1406660385600,0.012],[1406662185600,0.013],[1406663985600,0.012],[1406665785600,0.012],[1406667585600,0.012],[1406669385600,0.012]]}"
]
4

2 に答える 2

3

それらは実際には存在しません。引用符で区切られた文字列内に実際の引用符があることを示すためのものです。R はそのように出力します。

これは同様のものです:

> x
[1] "\""

しかしx、実際には1文字しかありません:

> nchar(x)
[1] 1

2つではありません。バックスラッシュはありません。生の文字列を出力するcatため、文字列で使用すると表示されます。cat

> cat(x)
"
于 2014-08-11T17:32:24.790 に答える
2

適切な回答を得るには、問題を再現するために必要な最小限の量だけを用意することが役立ちます。この場合、それは単なるtoJSON呼び出しであり、@MrFlick のコメントは的を射ています。cat文字列を表示するようなメソッドを使用する必要があります。

> toJSON(list(foo=1, bar=2))
[1] "{\"foo\":1,\"bar\":2}"

> cat(toJSON(list(foo=1, bar=2)))
{"foo":1,"bar":2}> 
于 2014-08-11T17:34:22.690 に答える