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.