11

我有一个如下所示的 json 字符串

[

    {
        "Name": "TEST",
        "deviceId": "",
        "CartId": "",
        "timestamp": 1383197265540,
        "FOOD": [],
        "City": "LONDON CA"
     }

]

我想从字符串中删除/删除第一个和最后一个方括号。那么我如何使用javascript从上面的字符串中删除第一个和最后一个方括号。

请帮我

4

4 回答 4

18

返回时使用:

return properties[0];

或者

var data = [

{
    "Name": "TEST",
    "deviceId": "",
    "CartId": "",
    "timestamp": 1383197265540,
    "FOOD": [],
    "City": "LONDON CA"
 }

]; // Or whatever the Json is
data = data[0];

或者,如果您通过另一个对象访问 json

var data = jsonObj[0];
于 2013-10-31T06:06:39.097 回答
12
var tmpStr = '[    
    {
        "Name": "TEST",
        "deviceId": "",
        "CartId": "",
        "timestamp": 1383197265540,
        "FOOD": [],
        "City": "LONDON CA"
     }

]';

var newStr = tmpStr.substring(1, tmpStr.length-1);

请参阅此代码笔示例

于 2013-10-31T06:10:10.973 回答
1

尝试这个:

var A = [{}]; var B = {}; A = [

    {
        "Name": "TEST",
        "deviceId": "",
        "CartId": "",
        "timestamp": 1383197265540,
        "FOOD": [],
        "City": "LONDON CA"
     }

]

B = A[0]; console.log(B); //required output
于 2013-10-31T06:11:46.040 回答
0

This should help:

.replace(/[[\]]/g, '')
于 2021-12-17T03:06:47.443 回答