简体   繁体   中英

javascript convert string to multi array array

I have an initial array (users) with multiple (string and numeric) arrays therein:

var users = [
['User: 10792 - Jack',45.7546,-117.807,2,'/res/smR11.gif'], ['User: 11248 - John',38.0867,131.976,3,'/res/smR08.gif']
];

I have a string of data from our server in the current form of:

newData = "['User: 18469 - Gary',-33.9399732539481,151.164383805489,3,'/res/markerw.gif'],['User: 10020 - Robert',40.6437563454472,-73.7593346140851,6,'/res/smR10.gif']";

I erase all existing data with users.length = 0;

I then need to insert the newData into the users array.

NOTE: I can obviously modify the server data into any other format that would be more suitable.

Any help would be greatly appreciated.

尝试这样的事情

     var users = JSON.parse(newData);

Your newData string looks very similar to the javascript above it. How about this...

 
 
 
 
  
  
  users = eval('[' + newData + ']');
 
 
  

[ EDIT ] As Bergi, rajeshkakawat and StephenJames pointed out, eval will work but is less secure.

See: JSON.parse vs. eval()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM