javascript - split this java script string -
this question has answer here:
i have following string
{ "browser": "firefox", "datetime": "28_may_2014_03_35_pm" } { "browser": "firefox", "datetime": "28_may_2014_03_36_pm" }
i want elements betwen opening {
, closing }
braces.
it looks quite json encoded objects, see there incongruences, please pay attention @ little edits in starting string:
json.parse('[{"browser": "firefox","datetime": "28_may_2014_03_35_pm"},{"browser": "firefox","datetime": "28_may_2014_03_36_pm"}]'); //this convert string actual javascript object..
edit:
if json in not proper encoded, you'll have fix here , there offcourse.. must add
- enclosing square brackets:
[{ ... }]
- a comma after every object ,
{}, {}
you can accomplish code
assume var_string contain string..
var_string = var_string.replace(/(\r\n|\n|\r)/gm,""); //removing line breaks; var_string += '[' + var_string + ']'; //adding square brackets var_string = var_string.replace(/}{/g, "},{"); //adding commas var_string = json.parse(var_string); //parsing object
Comments
Post a Comment