Home Web Front-end JS Tutorial Javascript generates json function code (can be decoded with php's json_decode)_json

Javascript generates json function code (can be decoded with php's json_decode)_json

May 16, 2016 pm 05:52 PM
json json_decode

But there is a problem
Although JS is very convenient to decode JSON, there seems to be no good way to encode it...
In line with the principle of being lazy if you can, and being as lazy as possible if you can’t be lazy, Gugou gave it a try
It really made it easier I discovered
You can decode it directly using json_decode in PHP, which is very convenient to use

Copy the code The code is 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");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]=a;
}
return "{" aa.join("," ) "}";
}
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Combination of golang WebSocket and JSON: realizing data transmission and parsing Combination of golang WebSocket and JSON: realizing data transmission and parsing Dec 17, 2023 pm 03:06 PM

Combination of golang WebSocket and JSON: realizing data transmission and parsing

What is the difference between MySQL5.7 and MySQL8.0? What is the difference between MySQL5.7 and MySQL8.0? Feb 19, 2024 am 11:21 AM

What is the difference between MySQL5.7 and MySQL8.0?

How to exclude a field from JSON using @Expose annotation in Java? How to exclude a field from JSON using @Expose annotation in Java? Sep 16, 2023 pm 09:49 PM

How to exclude a field from JSON using @Expose annotation in Java?

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization tips for converting PHP arrays to JSON

Pandas usage tutorial: Quick start for reading JSON files Pandas usage tutorial: Quick start for reading JSON files Jan 13, 2024 am 10:15 AM

Pandas usage tutorial: Quick start for reading JSON files

Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string Nov 18, 2023 pm 01:59 PM

Use the json.MarshalIndent function in golang to convert the structure into a formatted JSON string

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

How do annotations in the Jackson library control JSON serialization and deserialization?

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development

See all articles