Home  >  Article  >  Web Front-end  >  jQuery's method of processing json data to return array and output_jquery

jQuery's method of processing json data to return array and output_jquery

WBOY
WBOYOriginal
2016-05-16 16:10:121276browse

The example in this article describes the method of jQuery processing json data to return array and output. Share it with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:
/*print the json object
 *
 *$("selector").print_r_json(json,opts) : return formatted string (and print)
 *sprint_r_json : just return the string;
 *print_r_json : return the formatted string and print json data
 *contribute 明河
 *
 *auth iorichina
 *
 *example:
 *3 ways to use it
 *
 *
*/
$.fn.print_r_json = function(json,options){
    if(typeof(json)!="object") return false;
    var opts = $.extend({},$.fn.print_r_json.defaults,options);
    var data = '';
    if(opts.if_print)
    {
        data = $.sprint_r_json(json)
        $(this).html('
'+(opts.return_array?'Array':'JSON-DATA')+'
'+data);
    }
    if(opts.array)
    {
        return $.json_to_array(json);
    }
    return data;
};
$.fn.print_r_json.defaults =
{
    if_print : false,//if print or just return formatted string
    return_array : true //return an Array
};
$.extend({
    print_r_json : function(json)
    {
        if(typeof(json)=="object")
        {
            var text='
{
';
          document.write('
{
');
          for(var p in json)
          {
            if(typeof(json[p])=="object")
            {
              document.write('
["'+p+'"] => ');
                text+='
["'+p+'"] => '+$.print_r_json(json[p])+'
';
              document.write('
');
            }
            else
            {
                text+='
['+((/^\d+$/).test(p)?p:('"'+p+'"'))+'] => "'+json[p]+'"
';
              document.write('
['+p+'] => '+json[p]+'
');
            }
          } 
          text+='
}
';
          document.write('
}
');
          반환(텍스트);
        }
        그 외
        {
            document.write(json);
            반환(json);
        }
    },
    sprint_r_json : 함수(json)
    {
        if(typeof(json)=="객체")
        {
          var text = '
{
';
          for(json의 var p)
          {
            if(typeof(json[p])=="객체")
            {
              text = '
["' p '"] => ' $.sprint_r_json(json[p]) '
';
            }
            그 외
            {
              text = '
[' ((/^d $/).test(p)?p:('"' p '"')) '] => "' json[p] '"
';
            }
          } 
          text = '
}
';
          반환(텍스트);
        }
        그 외
        {
            반환(json);
        }
    },
    json_to_array : 함수(json)
    {
        if(typeof(json)=="객체")
        {
          var text = new Array();
          for(json의 var p)
          {
            if(typeof(json[p])=="객체")
            {
              텍스트[p] = $.json_to_array(json[p]);
            }
            그 외
            {
              텍스트[p] = json[p];
            }
          }
          반환(텍스트);
        }
        그 외
        {
            반환(json);
        }
    }
});

希望本文所述对大家의 jQuery는 매우 유용합니다.

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