将JavaScript对象数组转换为r编程语言中的JSON

km0tfn4u  于 2023-07-31  发布在  Java
关注(0)|答案(3)|浏览(110)

我需要将JSON对象传递给r编程中的一个函数。以下是可以传递的JavaScript对象的各种示例,这些对象需要在r中转换为JSON

Example1 = [
  {name: "Orange", data: {"Tuesday": 3, "Friday": 4}, stack: "fruit"},
  {name: "Apple", data: {"Tuesday": 1, "Friday": 8}, stack: "fruit"},
  {name: "Cucumber", data: {"Tuesday": 3, "Friday": 4}, stack: "vegetable"},
  {name: "Carrot", data: {"Tuesday": 1, "Friday": 8}, stack: "vegetable"}
]

Example2 = [["Washington", "1789-04-29", "1797-03-03"], ["Adams", "1797-03-03", "1801-03-03"]]

字符串
我需要帮助使用jsonlite包将它们转换为r中的JSON对象。

vh0rcniy

vh0rcniy1#

要获得“true”JSON字符串,可以使用jsonNormalize包。

library(jsonNormalize)

jsonNormalize(
  '[
  {name: "Orange", data: {"Tuesday": 3, "Friday": 4}, stack: "fruit"},
  {name: "Apple", data: {"Tuesday": 1, "Friday": 8}, stack: "fruit"},
  {name: "Cucumber", data: {"Tuesday": 3, "Friday": 4}, stack: "vegetable"},
  {name: "Carrot", data: {"Tuesday": 1, "Friday": 8}, stack: "vegetable"}
]', prettify = TRUE)
[
  {
    "name": "Orange",
    "data": {
      "Tuesday": 3,
      "Friday": 4
    },
    "stack": "fruit"
  },
  {
    "name": "Apple",
    "data": {
      "Tuesday": 1,
      "Friday": 8
    },
    "stack": "fruit"
  },
  {
    "name": "Cucumber",
    "data": {
      "Tuesday": 3,
      "Friday": 4
    },
    "stack": "vegetable"
  },
  {
    "name": "Carrot",
    "data": {
      "Tuesday": 1,
      "Friday": 8
    },
    "stack": "vegetable"
  }
]

jsonNormalize包还提供了一个RStudio插件,可以通过按钮对json文件进行规范化。

jdgnovmf

jdgnovmf2#

谢谢@margusl我刚刚解决了Example 1

jsonlite::toJSON('[
  {name: "Orange", data: {"Tuesday": 3, "Friday": 4}, stack: "fruit"},
  {name: "Apple", data: {"Tuesday": 1, "Friday": 8}, stack: "fruit"},
  {name: "Cucumber", data: {"Tuesday": 3, "Friday": 4}, stack: "vegetable"},
  {name: "Carrot", data: {"Tuesday": 1, "Friday": 8}, stack: "vegetable"}
]')

字符串
但是,这实际上不是我想要的。我希望我可以实现与在JavaScript中使用JSON.stringify()时相同的输出,其中键被引用。我希望我能在这里得到帮助

svgewumm

svgewumm3#

JavaScript代码可以用v8进行求值,该代码可以包括JSON.stringify(),但是对于一系列简单的赋值表达式,我们可以为$eval()方法设置serialize参数,它将返回JSON字符串:

library(V8)
#> Using V8 engine 9.1.269.38
js <- c(
  ex1 = 'Example1 = [
  {name: "Orange", data: {"Tuesday": 3, "Friday": 4}, stack: "fruit"},
  {name: "Apple", data: {"Tuesday": 1, "Friday": 8}, stack: "fruit"},
  {name: "Cucumber", data: {"Tuesday": 3, "Friday": 4}, stack: "vegetable"},
  {name: "Carrot", data: {"Tuesday": 1, "Friday": 8}, stack: "vegetable"}
]',
  ex2 = 'Example2 = [["Washington", "1789-04-29", "1797-03-03"], ["Adams", "1797-03-03", "1801-03-03"]]'
)

ctx <- v8()
# evaluate and serialize js return object
json_1 <- ctx$eval(js[1], serialize = TRUE) 
jsonlite::validate(json_1)
#> [1] TRUE
json_1
#> [1] "[{\"name\":\"Orange\",\"data\":{\"Tuesday\":3,\"Friday\":4},\"stack\":\"fruit\"},{\"name\":\"Apple\",\"data\":{\"Tuesday\":1,\"Friday\":8},\"stack\":\"fruit\"},{\"name\":\"Cucumber\",\"data\":{\"Tuesday\":3,\"Friday\":4},\"stack\":\"vegetable\"},{\"name\":\"Carrot\",\"data\":{\"Tuesday\":1,\"Friday\":8},\"stack\":\"vegetable\"}]"

# or apply ctx$eval() on the the list of expressions
json_lst <- lapply(js, ctx$eval, serialize = TRUE)
str(json_lst)
#> List of 2
#>  $ ex1: chr "[{\"name\":\"Orange\",\"data\":{\"Tuesday\":3,\"Friday\":4},\"stack\":\"fruit\"},{\"name\":\"Apple\",\"data\":{"| __truncated__
#>  $ ex2: chr "[[\"Washington\",\"1789-04-29\",\"1797-03-03\"],[\"Adams\",\"1797-03-03\",\"1801-03-03\"]]"

字符串
创建于2023-07-23,使用reprex v2.0.2

相关问题