json javascript

How to convert string to object in js

 
E.g
 
[javascript] 
var test='{ colkey: "col", colsinfo: "NameList" }'  
 
  var test='{ colkey: "col", colsinfo: "NameList" }' is obviously an object, but how to convert text to object. Use eval(); note that parentheses must be added, otherwise the conversion will fail 
 
 
 
 
convert text to object
 
 
 
[javascript] 
var test='{ colkey: "col", colsinfo: "NameList" }'    
  var obj2=eval("("+test+")");  
  alert(obj2.colsinfo);  
 
  var test='{ colkey: "col", colsinfo: "NameList" }'
  var obj2=eval("("+test+")");
  alert(obj2.colsinfo);
 
 
 
convert text to array
[javascript] 
var test='["colkey", "col", "colsinfo","NameList" ]'      
   var obj2=eval("("+test+")");  
   alert(obj2.length);  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040140&siteId=291194637