String and JSON cross-transfer

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.