Usage Analysis of javascript Object in json Format

  • 2021-07-01 06:43:15
  • OfStack

In this paper, an example is given to analyze the usage of javascript objects in json format. Share it for your reference, as follows:

Format:


objectName = {
 property1:value1,
 property2:value2,
  … ,
 propertyN:valueN
}

property is the property of the object, and value is the value of the object, which can be a string, a number, or one of the three objects

Example 1: Values are strings and numbers


var user={
 name:"user1",
 age:18
};

Example 2: Values are strings and objects


var user={ //user Itself is 1 Objects 
 name:"user1",
 job:{ //job Also 1 Objects 
  salary:3000,
  title:programmer
 }
}

Example 3: Values can also be functions


var user={ //user Is itself 1 Objects 
 name:"user1",
 age:18,
 getName:function(){ //getName As a method 
  return this.name;
 }
}

The following ZPVisitor class definition format is often used in real enterprise projects:


var ZPVisitor = {
 init : function(){
  var url = ZPConstant.AJAX_SERVER + "&a=hello" + "&sid=" + Math.random();
  $.getJSON(url,function(result){ // #TODO  Cross-domain request? 
   if (result.done) {
    var logininfo = result.data.name + "  How do you do !";
    if (!result.data.login) {
    logininfo += "&nbsp;<a href=\"" + ZPConstant.MYZP_DOMAIN + "index.php?c=member&a=login\"> Login </a>/<a href=\"" +
    ZPConstant.MYZP_DOMAIN + "index.php?c=member&a=regist\"> Registration </a>";
    } else {
    logininfo += "&nbsp;<a href=\"" + ZPConstant.MYZP_DOMAIN + "index.php?c=member&a=logout\"> Logoff </a>";
    }
    $("#login-info").html(logininfo);
   } else {
    popAlert(result.msg);
   }
  }); // End getJSON
 }, // End init Method 
 //  Collect merchandise 
 collect : function(pid) {
   var pid = parseInt(pid);
   if (isNaN(pid)) return false;
   var url = ZPConstant.AJAX_SERVER + "&a=collect&pid=" + pid + "&sid=" + Math.random();
   $.getJSON(url,function(result){
    if(result.done){
     popAlert(result.msg);
    } else {
     popAlert(result.msg);
    }
   });// End getJSON Method 
 },//  End collect Method 
 //  Browse history 
 vHistory :{
  get:function(callback){
   var items_str = getCookie(ZPConstant.VIEW_HISTORY_CKNAME);
   if (items_str == null) return;
   var items = items_str.split(",").reverse().join(","),
   url = ZPConstant.AJAX_SERVER + "&a=viewHistory" + "&items="+ items + "&sid=" + Math.random();
   // TODO  Request data from the server 
   $.getJSON(url,function(response){
     if(response.done){
      callback(response.data,items);
    } else {
      return false;
    }
   });// End getJSON Method 
  },// End get Method 
  add:function(pid){
   var pid = parseInt(pid);
   if (isNaN(pid)) return;
   var cookieStr = getCookie(ZPConstant.VIEW_HISTORY_CKNAME);
   if (cookieStr) {
    var pids = cookieStr.split(","),_tempPids = [];
    for (var i=0;i<pids.length;i++){
     if (pids[i] != pid) { //  Remove previously written data if it already exists 
      _tempPids.push(parseInt(pids[i]));
     }
    }
    _tempPids.push(pid);//  Put new data at the end of the array 
    setCookie(ZPConstant.VIEW_HISTORY_CKNAME, _tempPids.reverse().slice(0,5).reverse().join(","));
   } else {
    setCookie(ZPConstant.VIEW_HISTORY_CKNAME, pid);
   }
  },//  End add Method 
  clear:function(){
    setCookie(ZPConstant.VIEW_HISTORY_CKNAME, '', -100);
  }
 },// End vHistory
 //  Deleted item record of shopping cart 
 dropHistory: function(spid){
  var cookieStr = getCookie(ZPConstant.CARTDEL_CKNAME);
  if (cookieStr){
   var idsDel = cookieStr.split(",");
   for (var i=0;i<idsDel.length;i++){
     idsDel[i] = parseInt(idsDel[i]);
   }
   if ($.inArray(spid,idsDel) == -1){
    idsDel.push(spid);
    setCookie(ZPConstant.CARTDEL_CKNAME, idsDel.join(","));
   }
  } else {
   setCookie(ZPConstant.CARTDEL_CKNAME, spid);
  }
 }// End dropHistory
}// End ZPVisitor

PS: Here are some json online tools recommended for you. I believe you can use them in future development:

Online JSON code verification, verification, beautification and formatting tools:
http://tools.ofstack.com/code/json

JSON Online Formatting Tool:
http://tools.ofstack.com/code/jsonformat

Online XML/JSON Interconversion Tool:
http://tools.ofstack.com/code/xmljson

json code online formatting/beautification/compression/editing/conversion tool:
http://tools.ofstack.com/code/jsoncodeformat

C Language Style/HTML/CSS/json Code Formatting and Beautification Tool:
http://tools.ofstack.com/code/ccode_html_css_json

For more readers interested in JavaScript related contents, please check the special topics of this site: "Summary of json Operation Skills in JavaScript", "Summary of JavaScript Switching Special Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills", "Summary of JavaScript Error and Debugging Skills", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Traversal Algorithm and Skills" and "Summary of JavaScript Mathematical Operation Usage"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: