Home  >  Article  >  Web Front-end  >  Summary of jQuery operation json methods

Summary of jQuery operation json methods

php中世界最好的语言
php中世界最好的语言Original
2018-04-25 13:44:104566browse

This time I will bring you a summary of the jQuery operation json method. What are the precautions for jQuery operation json. The following is a practical case, let's take a look.

In projects, it is often used to obtain a certain value in json, or to dynamically create a json object. Today I simply made a general js

/**
 * json工具
 */
var JsonUtil = (function(){
  return {
    /**
     * 获取json中的单个值
     */
    getValue:function(jsonObject,name){
      var value = "";
      $.each(jsonObject,function(n,v){
        if(name == n){
          value = v;
          return false;
        }
      });
      return value;
    },
    /**
     * 获取json中的name 以数组形式返回
     */
    getNames:function(jsonObject){
      var names = [];
      $.each(jsonObject,function(n,v){
        names.push(n);
      });
      return names;
    },
    /**
     * 创建json对象
     */
    createJsonObject:function(){
      this.jsonObectArr = [];
    }
  }
})();
/**
 * 创建json的nam和value的名值对字符串
 */
JsonUtil.createJsonObject.prototype.createJsonStr = function (name,value){
  if (typeof value == 'string'){
    this.jsonObectArr.push("\""+name+"\":"+"\""+value+"\"");
  }else{
    this.jsonObectArr.push("\""+name+"\":"+value);
  }
}
/**
 * 获取json对象
 */
JsonUtil.createJsonObject.prototype.getJson = function (){
  var str = "{"+this.jsonObectArr.join(',')+"}";
  return $.parseJSON(str);
}

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of Jquery parsing Json string and Json array methods

.net entity class and json interaction Summary of conversion methods

The above is the detailed content of Summary of jQuery operation json methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn