>  기사  >  웹 프론트엔드  >  배열 및 output_jquery를 반환하기 위해 json 데이터를 처리하는 jQuery의 방법

배열 및 output_jquery를 반환하기 위해 json 데이터를 처리하는 jQuery의 방법

WBOY
WBOY원래의
2016-05-16 16:10:121248검색

이 글의 예시에서는 jQuery가 json 데이터를 처리하여 배열과 출력을 반환하는 방법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 구체적인 구현 방법은 다음과 같습니다.

코드 복사 코드는 다음과 같습니다.
/*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는 매우 유용합니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.