R create JSON in R

2019-08-12 20:16发布

问题:

I want to get to get the values from this JSON string: { "item": [ { "value": 142 } , { "value": 200 } ] }

R code:

library(rjson)
item <- list(item=c(value= 142,value=200))
toJSON(item)

And I have:

> toJSON(item)
[1] "{\"item\":{\"value\":142,\"value\":200}}"

How do I get the values from the json string in R?

回答1:

Discussion of I() and what it does is in the RJSONIO documentation, but that's what you are looking for...

library("RJSONIO")
item <- list(item=I(list(list(value= 142),list(value=200))))
> toJSON(item)
[1] "{\n \"item\": [\n {\n \"value\":    142 \n},\n{\n \"value\":    200 \n} \n] \n}"


标签: r rjson