2

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Shen
  • 183
  • 1
  • 10
  • Does `item` _have_ to be in that structure or can you do something like what @cory suggests in his answer. Also, you should show a `library(rjson)` in your question text even if you have it as a tag. There are a plethora of JSON packages for R. – hrbrmstr Jun 10 '15 at 11:43

1 Answers1

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}"
cory
  • 6,529
  • 3
  • 21
  • 41