开发者

How can I produce valid JSON using the toJSON method from rjson/RJSONIO in R?

开发者 https://www.devze.com 2023-03-09 22:44 出处:网络
given the following R code: library(rjson) x <- c(3:5) toJSON(x) it produces: [1] \"[3,4,5]\" when I am expecting something like:

given the following R code:

library(rjson)
x <- c(3:5)
toJSON(x)

it produces:

[1] "[3,4,5]"

when I am expecting something like:

"[3,4,5]"

I am a new to 开发者_开发问答R, I guess this is something easy to do, but I had have no luck finding a solution.


I believe that [1] is just the R Console's output indicating that "[3,4,5]" is the first element in that vector. The actual result stored is in fact "[3,4,5]". Try this:

cat(toJSON(x))

So, for instance, if you stored the JSON result in a new variable, and passed it on to something else, the '[1]' doesn't ride along, just the part you want. It's only for display purposes in the console.

0

精彩评论

暂无评论...
验证码 换一张
取 消