Javascript code used to generate json functions (php json_decode can be used to decode) _ json

Source: Internet
Author: User
I have been fascinated by JSON these days, and even some XML work in the project has been handed over to JSON. JSON is much more convenient than XML, but there is a problem.
Although JS decoding JSON is very convenient, there seems to be no good way to code it...
In the principle that you can be either lazy or lazy
I found it.
Json_decode can be directly used in PHP for decoding, which is quite convenient to use.

The Code is as follows:


Function json_encode_js (aaa ){
Function je (str ){
Var a = [], I = 0;
Var pcs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
For (; 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"); return ;}
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] =;
}
Return "{" + aa. join (",") + "}";
}

Related Article

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.