JQuery's method of processing the returned array and output of json data

Source: Internet
Author: User

JQuery's method of processing the returned array and output of json data

This article describes how jQuery processes the returned arrays and outputs of json data. It involves jQuery Array Operations and json techniques. For more information, see

 

 

This example describes how jQuery processes the returned arrays and outputs of json data. Share it with you for your reference. The specific implementation method is as follows:

 

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
* Contri Ming River
*
* Auth iorichina
*
* Example:
* 3 ways to use it
* <Script language = "javascript">
* $ ("Selector "). print_r_json ({"a": "aa", "d": {"ef": {"a": "d", "d": ["", "B"]}, "ed": "dd"}, "g": "g" },{ if_print: true, return_array: true });
* Document. write ($. sprint_r_json ({"a": "aa", "d": {"ef": {"a": "d", "d": ["", "B"]}, "ed": "dd"}, "g": "g "}));
* $. Print_r_json ({"a": "aa", "d": {"ef": {"a": "d", "d": ["", "B"]}, "ed": "dd"}, "g": "g "});
* </Script>
*
*/
$. 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)
Authorization (this).html ('<div style = "font-weight: bold">' + (opts. return_array? 'Array': 'json-data') + '</div>' + 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 = '<div style = "font-weight: bold;" >{</div> <div style = "margin-left: 25px;"> ';
Document. write ('<div style = "font-weight: bold;" >{</div> <div style = "margin-left: 25px;"> ');
For (var p in json)
{
If (typeof (json [p]) = "object ")
{
Document. write ('<div> ["' + p + '"] => ');
Text + = '<div> ["' + p + '"] =>' + $. print_r_json (json [p]) + '</div> ';
Document. write ('</div> ');
}
Else
{
Text + = '<div> [' + (/^ \ d + $/). test (p )? P :( '"' + p + '"') + '] => "' + json [p] + '" </div> ';
Document. write ('<div> [' + p + '] =>' + json [p] + '</div> ');
}
}
Text + = '</div> <div style = "font-weight: bold;" >}</div> ';
Document. write ('</div> <div style = "font-weight: bold;" >}</div> ');
Return (text );
}
Else
{
Document. write (json );
Return (json );
}
},
Sprint_r_json: function (json)
{
If (typeof (json) = "object ")
{
Var text = '<div style = "font-weight: bold;" >{</div> <div style = "margin-left: 25px;"> ';
For (var p in json)
{
If (typeof (json [p]) = "object ")
{
Text + = '<div> ["' + p + '"] =>' + $. sprint_r_json (json [p]) + '</div> ';
}
Else
{
Text + = '<div> [' + (/^ \ d + $/). test (p )? P :( '"' + p + '"') + '] => "' + json [p] + '" </div> ';
}
}
Text + = '</div> <div style = "font-weight: bold;" >}</div> ';
Return (text );
}
Else
{
Return (json );
}
},
Json_to_array: function (json)
{
If (typeof (json) = "object ")
{
Var text = new Array ();
For (var p in json)
{
If (typeof (json [p]) = "object ")
{
Text [p] = $. json_to_array (json [p]);
}
Else
{
Text [p] = json [p];
}
}
Return (text );
}
Else
{
Return (json );
}
}
});

 

I hope this article will help you with jQuery programming.

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.