如何通过保持JSON结构在R中合并两个JSON对象? [英] How to merge two JSON objects in R by keeping the JSON structure?

查看:67
本文介绍了如何通过保持JSON结构在R中合并两个JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有两个单独的对象,它们具有JSON格式,并且我试图将其结构中的两个JSON objects 转换为单个R对象.

I have two separate objects in R which have a JSON format and I'm trying to convert into a single R objects with two JSON objects in it's structure.

当我创建包含两个对象的列表然后与toJSON()连接时,它将创建两个JSON 对象,但是我丢失了JSON子结构,并且对象是扁平的.

When I create a list with the two objects then concatenate with toJSON() it creates the two JSON objects but I lost the JSON sub structure and objects are flat.

例如:

json <- list(obj1, obj2)
names(json) <- c("object1", "object2")  
json <- toJSON(json)

结果如下:

{
    "object1": ["{\"cum\":[[[1421020800000, -0.0618],[1422835200000, 0.3907] ... "],
    "object2": ["{\"cum\":[[[1421020800000, -0.015],[1422835200000, 0.3447] ... "]
}

这是我的目标结构.

obj1:

{
    "cum": [
        [
            [1421020800000, -0.0618],
            [1422835200000, 0.3907]
        ]
    ],
    "alloc": {
        "Current": [0.36, 0.725, 0.074, 0.473, 0.029, 10, 0.46, 0.414, 0.965],
    },
    "time": [14],
    "position": [15.14]
}

obj2:

{
    "cum": [
        [
            [1421020800000, -0.015],
            [1422835200000, 0.3447]
        ]
    ],
    "alloc": {
        "Current": [0.6, 0.5, 0.04, 0.3, 0.09, 1, 0.6, 0.44, 0.5],
    },
    "time": [19],
    "position": [1.09]
}

这就是我要实现的目标:

And this is what I'm trying to achieve :

{
    "object1": {
        "cum": [
            [
                [1421020800000, -0.0618],
                [1422835200000, 0.3907]
            ]
        ],
        "alloc": {
            "Current": [0.36, 0.725, 0.074, 0.473, 0.029, 10, 0.46, 0.414, 0.965]
        },
        "time": [14],
        "position": [15.14]
    },

    "object2": {
        "cum": [
            [
                [1421020800000, -0.015],
                [1422835200000, 0.3447]
            ]
        ],
        "alloc": {
            "Current": [0.6, 0.5, 0.04, 0.3, 0.09, 1, 0.6, 0.44, 0.5]
        },
        "time": [19],
        "position": [1.09]
    }
}

谢谢

推荐答案

我只是使用paste0而不是list()toJSON来找到了自己的问题的答案.

I just found the answer to my own question, by simply using paste0 instead of list() and toJSON.

json <- paste0('{"object1":', json1, ',"object2":', json2, '}' )

这篇关于如何通过保持JSON结构在R中合并两个JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆