Json.parse () and json.stringify () usage

Source: Internet
Author: User

Json.parse () "Parse a JSON object from a string"

Json.stringify () "Parse out a string from an object"

To do the project landing function, the use of the H5 Sessionstorage method, will be logged back after the information to save to the browser. But the return information is an object, so you need to use

The Json.stringify method converts an object into a string and then stores it. as the following code
//Click to loginfunction Fun_login (_type) {//Check Entry items:  varErr =Checklogin (); if(ERR) {alert (ERR); return; }  varFormData = {    "Logintype": _type,"LoginName": $(". Login-right input[name= ' loginname ']"). Val (),"Password": $(". Login-right input[name= ' password ']"). Val ()}; $.ajax ({type:'Post', URL:".. /spirit/login", Data:formdata, DataType:'JSON', Success:function (data) {Console.log (data); if(Data.returncode = ="xx") {IsLogin="1"; varMemberobjstr =json.stringify (Data.memberinfo); Sessionstorage.setitem ("MemberInfo", MEMBEROBJSTR); Sessionstorage.setitem ("pwd", $(". Login-right input[name= ' password ']"). Val ()); $("#signBox"). HTML (""); $("#signModal"). Modal ("Hide");      Fillmembermes (MEMBEROBJSTR); } Else{alert ("Login failed:"+data.messageinfo); }}, Error:function (XMLHttpRequest, Textstatus, Errorthrown) {//Failure callback function      varm ="system error: \nstatu="+ Xmlhttprequest.status +"\nreadystate="+ Xmlhttprequest.readystate +"\ntext="+ Textstatus +"\nerrthrown="+Errorthrown;    Alert (m); }  });

When the login after the refresh, the browser to determine the information stored in the absence, if any, then put personal information on the top of the site, if not, then remind login. At this time to fill the information with the object to fill, so the Json.parse method to convert the string to an object and then operate;

// Loading Information $ (document). Ready (function (e) {  var memberobjstr = Sessionstorage.getitem ("  MemberInfo");   if (MEMBEROBJSTR) fillmembermes (MEMBEROBJSTR); });

Fill header information after landing
function Fillmembermes (data) {
var data = json.parse (data);
Console.log (data);
if (data) {
if (Data.memberclass = = ' 1 ') Data.membertype = "Individual Member";
else {
if (Data.membertype = = ' 1 ') Data.membertype = "Supplier Member";
Else
if (Data.membertype = = ' 2 ') Data.membertype = "Purchasing Unit Member";
Else
Data.membertype = "Unknown type member";
}
var str = ' <span class= ' headimg "> ' + data.username + ' </span> ' +
' <span> ' + ' (' + Data.membertype + ') ' + ' </span> ' +
' <a onclick= ' toadmin () "style=" color: #fc771f;p adding-left:15px;cursor:pointer; " > Log in to the management side </a> ';
$ ("#signBox"). html (str);
};
}

Json.parse () and json.stringify () usage

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.