one, JSON to string
varstr = "1: Test 1-1; 2: Test 1-2; 3: Test 1-3";//string varSTR0 = Str.split (';');//string to Array ["1: Test 1-1", "2: Test 1-2", "3: Test 1-3"] /*Method One*/ //using $.map and Json.parse (' str ') method to achieve varStr1_1 = $.map (STR0,function(value, index) {if(typeofValue = = = "string"){ varStr_ = Value.split (': ')); return"' + str_[0] + '": "' + str_[1] + '" '; } }) //format quoted array: ["" 1 ":" Test 1-1 "", "" 2 ":" Test 1-2 "," "3": "Test 1-3"] varStr1_2 = "{" + str1_1.join (', ') + "}";//Strings formatted string: {"1": "Test 1-1", "2": "Test 1-2", "3": "Test 1-3"}Console.log (Json.parse (str1_2));//Output Results object:{1: "Test 1-1", 2: "Test 1-2", 3: "Test 1-3"} /*Method Two*/ //use native ForEach with eval (' ({"+str+}) ') method to implementStr0.foreach (function(Value,index,array) {//ForEach incompatible ie8-can be replaced with $.map of method one varStr_=value.split (': ')); Array[index]= "\" "+str_[0]+" \ ": \" "+str_[1]+" \ ""; }); varStr2_1=eval ("({" +str0.join (', ') + "})"); Console.log (Str2_1); //Output Results object:{1: "Test 1-1", 2: "Test 1-2", 3: "Test 1-3"}
String and JSON cross-transfer