如何在字符串对象的两个对象之间添加逗号,而不是最后一个对象

Javascript: How to add comma in between two object in string object but not last object

本文关键字:对象 添加 一个对象 最后 之间 两个 字符串      更新时间:2023-09-26

我有一个字符串对象,但是存储在另一个类的变量传递中。如何在两个宾语之间加逗号,而不是最后一个宾语。下面是我的代码。

{
        "id":"57e4d12e53a5a",
        "body":"asdas",
        "published":"Fri, 
        23 Sep 2016 06:52:30 +0000",
        "type":"chat-message",
        "actor":
            {
            "displayName":"beau",
            "objectType":"person",
            "image":
                {
                "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
                "width":48,"height":48
                }
            }
    }
    {
        "id":"57e4d51165d97",
        "body":"jackiechanSADAS",
        "published":"Fri, 23 Sep 2016 07:09:05 +0000",
        "type":"chat-message",
        "actor":
            {
                "displayName":"beau",
                "objectType":"person",
                "image":
                    {
                        "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
                        "width":48,
                        "height":48
                    }
            }
    }
    {
        "id":"asas",
        "body":"peterting",
        "published":"Fri, 23 Sep 2016 07:09:05 +0000",
        "type":"chat-message",
        "actor":
            {
                "displayName":"beau",
                "objectType":"person",
                "image":
                    {
                        "url":"http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?s=80&d=mm&r=g",
                        "width":48,
                        "height":48
                    }
            }
    }

我如何添加逗号","之间的对象,但不是最后一个对象使用编程,因为整个东西我存储它作为字符串,没有","我不能解析它为对象。

您可以使用正则表达式:

JSON.parse('[' + string.replace(/}'s*{/g, '},{') + ']');