在R中的文本字符串中添加特殊字符

时间:2017-08-24 21:06:55

标签: json r api jsonlite

我正在尝试使用R创建一些API请求并使用jsonlite包来执行此操作。我的JSON请求中有一个名为selected的元素需要设置为“:: total ::”:“”。我尝试过的所有内容都会导致添加“\”。我通常得到这样的东西......“:: total :: \”:\“”。关于如何解决这个问题的任何想法?

这是我的代码:

          reportsuite.id = "myRSID" 
      date.from = 2017-06-23
      date.to = 2017-08-23
      metrics = c("pageviews", "visits", "uniquevisitors")
      elements = myVar
      top = 0
      start = 0
      selected = '::total::": "'
      search = c()
      search.type = "or"
      date.granularity = "day"
      segment.id = ""
      segment.inline = ""
      classification = character(0)
      anomaly.detection = FALSE
      data.current = TRUE
      expedite = FALSE
      interval.seconds = 5 
      max.attempts = 120
      validate = TRUE
      enqueueOnly = FALSE


        report.description <- c()
        report.description$reportDescription <- c(data.frame(matrix(ncol = 0, 
                                                                    nrow = 1)))
        report.description$reportDescription$dateFrom <- unbox(date.from)
        report.description$reportDescription$dateTo <- unbox(date.to)
        report.description$reportDescription$reportSuiteID <- unbox(reportsuite.id)
        report.description$reportDescription$dateGranularity <- unbox(date.granularity)
        report.description$reportDescription$elementDataEncoding <- unbox("utf8")
        if (segment.inline != "") {
          report.description$reportDescription$segments <- list(segment.inline)
        }
        if (as.list(segment.id)[1] == "") {
          report.description$reportDescription$segment_id <- unbox(segment.id)
        } else {
          report.description$reportDescription$segments <- data.frame(id = segment.id)
        }
        if (anomaly.detection == TRUE) {
          report.description$reportDescription$anomalyDetection <- unbox(anomaly.detection)
        }
        if (data.current == TRUE) {
          report.description$reportDescription$currentData <- unbox(data.current)
        }
        if (expedite == TRUE) {
          report.description$reportDescription$expedite <- unbox(expedite)
        }
        report.description$reportDescription$metrics = data.frame(id = metrics)
        elements.formatted <- list()
        i <- 0
        for (element in elements) {
          i <- i + 1
          if (i == 1) {
            working.element <- list(id = unbox(element), top = unbox(top[1]), 
                                    startingWith = unbox(start))
            if (length(selected) != 0) {
              working.element[["selected"]] <- selected
            }
            if (length(search) != 0) {
              working.element[["search"]] <- list(type = unbox(search.type), 
                                                  keywords = search)
            } 
          } else {
            if (length(top) >= i) {
              working.element <- list(id = unbox(element), 
                                      top = unbox(top[i]))
            } else {
              working.element <- list(id = unbox(element), 
                                      top = unbox("50000"))
            }
          }
          if (length(classification) >= i) {
            working.element[["classification"]] <- unbox(classification[i])
          }
          if (length(elements.formatted) > 0) {
            elements.formatted <- append(elements.formatted, 
                                         list(working.element))
          } else {
            elements.formatted <- list(working.element)
          }
        }
        report.description$reportDescription$elements <- elements.formatted

结果:

的toJSON(report.description)

  

{ “reportDescription”:{ “dateFrom”: “2017年6月23日”, “dateTo”: “2017年8月23日”, “reportSuiteID”: “myRSID”, “dateGranularity”: “日”,” elementDataEncoding “:” UTF8" , “SEGMENT_ID”: “”, “CURRENTDATA”:真正的 “指标”:[{ “ID”: “浏览量”},{ “ID”: “访问”},{ “ID”: “uniquevisitors”}],“elements”:[{“id”:“evar32”,“top”:0,“startingWith”:0,“selected”:[“:: total :: \”:\“”] }]}}

谢谢! 杰斯

0 个答案:

没有答案