我用R创建了三个不同的json对象。它们看起来像(我放了每个对象的一小部分):
JSON 1:
[
{
"id": "user_F_1",
"group": {
"age": "32"
},
"activity": {
"sport": "football"
},
"scores": {
"2016": "85",
"2017": "87"
}
},
{
"id": "user_F_2",
"group": {
"age": "32"
},
"activity": {
"sport": "hockey"
},
"scores": {
"2016": "62",
"2017": "75"
}
}
]
JSON 2:
[
{
"id": "user_H_1",
"gender": "male",
"region": {
"country": "Finland"
}
},
{
"id": "user_H_2",
"gender": "female",
"region": {
"country": "Greece"
}
}
]
JSON 3:
[
{
"id": "user_Z_1",
"gender": "female",
"age": "35",
"data": {
"continent": "Europe",
"country": "France",
"teamgroup": 3
}
},
{
"id": "user_Z_2",
"gender": "female",
"age": "46",
"data": {
"continent": "Asia",
"country": "China",
"teamgroup": 17
}
}
]
我需要将它们合并为一个json对象。 按照此question/27524122中的建议,我尝试做:
jsonl <- list(fromJSON(json1), fromJSON(json2), fromJSON(json3))
jsonc <- toJSON(jsonl, pretty=TRUE)
但是它将三个连接在一起,而不是合并。我的意思是,它返回:
[
{
"id": "user_F_1",
"group": {
"age": "32"
},
"activity": {
"sport": "football"
},
"scores": {
"2016": "85",
"2017": "87"
}
},
{
"id": "user_F_2",
"group": {
"age": "32"
},
"activity": {
"sport": "hockey"
},
"scores": {
"2016": "62",
"2017": "75"
}
}
],
[
{
"id": "user_H_1",
"gender": "male",
"region": {
"country": "Finland"
}
},
{
"id": "user_H_2",
"gender": "female",
"region": {
"country": "Greece"
}
}
],
[
{
"id": "user_Z_1",
"gender": "female",
"age": "35",
"data": {
"continent": "Europe",
"country": "France",
"teamgroup": 3
}
},
{
"id": "user_Z_2",
"gender": "female",
"age": "46",
"data": {
"continent": "Asia",
"country": "China",
"teamgroup": 17
}
}
]
我需要:
[
{
"id": "user_F_1",
"group": {
"age": "32"
},
"activity": {
"sport": "football"
},
"scores": {
"2016": "85",
"2017": "87"
}
},
{
"id": "user_F_2",
"group": {
"age": "32"
},
"activity": {
"sport": "hockey"
},
"scores": {
"2016": "62",
"2017": "75"
}
},
{
"id": "user_H_1",
"gender": "male",
"region": {
"country": "Finland"
}
},
{
"id": "user_H_2",
"gender": "female",
"region": {
"country": "Greece"
}
},
{
"id": "user_Z_1",
"gender": "female",
"age": "35",
"data": {
"continent": "Europe",
"country": "France",
"teamgroup": 3
}
},
{
"id": "user_Z_2",
"gender": "female",
"age": "46",
"data": {
"continent": "Asia",
"country": "China",
"teamgroup": 17
}
}
]
即在不同的json之间没有方括号。方括号内的所有元素:[{...},{...},{...},{...},{...},{...}]
。
我在此question/47983058中也尝试了paste0
选项,但是它也不起作用。
我该如何实现?
答案 0 :(得分:0)
似乎@objc func resetPoolChar(){charPoolBoxForMenuItem.stringValue = "abc"}
let charPoolMenuItem = NSMenuItem()
charPoolMenuItem.title = "Character Pool"
charPoolMenuItem.view = charPoolBoxForMenuItem
charPoolMenuItem.action = #selector(resetPoolChar)
charPoolBoxForMenuItem.placeholderString = "Character Pool"
charPoolBoxForMenuItem.frame = CGRect(x: 30, y: 0, width: 400, height: 22)
返回了一个列表。要串联列表,应使用fromJSON
函数。
c
示例:
json_all <- c(fromJSON(json1), fromJSON(json2), fromJSON(json3))
jsonc <- toJSON(json_all, pretty=TRUE)
合并:
json1 <- '
[
{
"id": "user_H_1",
"gender": "male"
}
]'
json2 <- '
[
{
"id": "user_H_1e",
"gender": "female"
},
{
"id": "user_H_2e",
"age": "62"
}
]'
输出:
library(jsonlite)
toJSON(c(fromJSON(json1), fromJSON(json2)), pretty=TRUE)
答案 1 :(得分:0)
在@Jet回答之后,我通过以下方法解决了我的问题:首先删除括号,然后连接所有json,最后在结果json的开头和结尾处再次添加括号:
cat("[",gsub("\\[|\\]", "",json1), ",", gsub("\\[|\\]", "",json2), ",", gsub("\\[|\\]", "",json3),"]" )
如果要将其写入json文件中,则:
fileConn<-file("Output.json")
writeLines(paste0("[", gsub("\\[|\\]", "",json1), ",", gsub("\\[|\\]", "",json2), ",", gsub("\\[|\\]", "",json3),"]"), fileConn)
close(fileConn)