最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove brackets from JSON - Stack Overflow

programmeradmin4浏览0评论

I have this JSON data:

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

]';

How can I delete the brackets?

Here is more of my JSON file:

[{"arrivee":false,"des":"Ceintures De Sécurité Conducteur","code":"nn","depart":true},
 {"arrivee":true,"des"‌​‌​:"Lecteur Tachygraphe","code":"nn","depart":false}
 {"arrivee":false,"d‌​‌​es":"Ceintures De Sécurités Passagères","code":"nn","depart":true},
 {"arrivee":true,"des"‌​‌​:"Climatisation","‌​co‌​de":"nn","depart‌​":fa‌​lse}]

I have this JSON data:

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

]';

How can I delete the brackets?

Here is more of my JSON file:

[{"arrivee":false,"des":"Ceintures De Sécurité Conducteur","code":"nn","depart":true},
 {"arrivee":true,"des"‌​‌​:"Lecteur Tachygraphe","code":"nn","depart":false}
 {"arrivee":false,"d‌​‌​es":"Ceintures De Sécurités Passagères","code":"nn","depart":true},
 {"arrivee":true,"des"‌​‌​:"Climatisation","‌​co‌​de":"nn","depart‌​":fa‌​lse}]
Share Improve this question edited Oct 16, 2016 at 22:16 Heretic Monkey 12.1k7 gold badges61 silver badges131 bronze badges asked Oct 16, 2016 at 17:14 user6893264user6893264
Add a ment  | 

3 Answers 3

Reset to default 3

Parse the JSON string and use the first element of the array.

var tmpStr = '[{"Name": "TEST","deviceId": "", "CartId": "", "timestamp": 383197265540, "FOOD": [], "City": "LONDON CA" }]',
    object = JSON.parse(tmpStr)[0];

console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

You dont have to delete brackes, just do this,

var result = tmpStr[0];

Description:
You wouldn't want to, this is a JSON array of Objects

[ // this starts an array
    { // this starts an object
        "Name": "TEST", // this is a property named 'Name'
        "deviceId": "", // this is a property named 'deviceId'
        "CartId": "", // this is a property named 'CartId'
        "timestamp": 1383197265540, // this is a property named 'timestamp'
        "FOOD": [], // this is a property named 'FOOD'
        "City": "LONDON CA" // this is a property named 'City'
    } // this ends an object
] // this ends an array
发布评论

评论列表(0)

  1. 暂无评论