JavaScript generates JSON's function code (which can be decoded using PHP's Json_decode) _json

Source: Internet
Author: User
But there's a problem.
Although JS is very handy for decoding JSON, coding does not seem to be a good idea ...
In the spirit can lazy is lazy, not lazy also as lazy as possible principle, the old dog a bit
It really made me find out.
PHP directly with Json_decode can be decoded, it is very convenient to use
Copy Code code as follows:

function Json_encode_js (AAA) {
Function je (str) {
var a=[],i=0;
var pcs= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (; i<str.length;i++) {
if (Pcs.indexof (str[i)) = = 1)
a[i]= "\\u" + ("0000" +str.charcodeat (i). toString (16)). Slice (-4);
Else
A[i]=str[i];
}
Return A.join ("");
}
var i,s,a,aa=[];
if (typeof (AAA)!= "Object") {alert ("ERROR json");
for (i in AAA) {
S=aaa[i];
a= ' "' +je (i) + '": ';
if (typeof (s) = = ' object ') {
A+=json_encode_js (s);
}else{
if (typeof (s) = = ' string ')
a+= ' "' +je (s) + '";
Else if (typeof (s) = = ' number ')
A+=s;
}
Aa[aa.length]=a;
}
Return "{" +aa.join (",") + "}";
}

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.