When using jsonlite in R, how do I specify that only some of the entries are to be treated as arrays?

后端 未结 1 1042
太阳男子
太阳男子 2021-01-21 15:45

I have the following code:

# install.packages(\"jsonlite\")
require(\"jsonlite\")
x = list(
    test = \"my_test\",
    data = c(1, 2, 3)
)
toJSON(x)


        
相关标签:
1条回答
  • 2021-01-21 16:34

    The argument auto_unbox=TRUE did the trick:

    automatically unbox all atomic vectors of length 1. It is usually safer to avoid this and instead use the unbox function to unbox individual elements. An exception is that objects of class AsIs (i.e. wrapped in I()) are not automatically unboxed. This is a way to mark single values as length-1 arrays.

    I.e., the solution was toJSON(x, auto_unbox=TRUE), which returns what I expected:

    {"test":"my_test","data":[1,2,3]}
    
    0 讨论(0)
提交回复
热议问题