gpt4 book ai didi

Javascript:如何在字符串对象中的两个对象之间添加逗号但不添加最后一个对象

转载 作者:行者123 更新时间:2023-11-28 12:20:23 25 4
gpt4 key购买 nike

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

{
"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, '},{') + ']');

关于Javascript:如何在字符串对象中的两个对象之间添加逗号但不添加最后一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39655387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com