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 

json syntax


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -