How to convert strings into objects and arrays in Js sample code

Obviously an object, but how to turn text into an object. Use eval(); note that parentheses must be added, otherwise the conversion will fail

  • convert text to object
var test='{ colkey: "col", colsinfo: "NameList" }'
var obj2=eval("("+test+")");
alert(obj2.colsinfo);
  • convert text to array
var test='["colkey", "col", "colsinfo","NameList" ]'
var obj2=eval("("+test+")");
alert(obj2.length);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325708793&siteId=291194637