String to JSON Object using backslash escapes (Encode and Decode)
http://json.org
A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.
In ECMA262 version 5
<script type="text/javascript">
var j='{"acct_id":100001,"acct_name":"na\\"me","email":"Jet\'mail"}';
var o=JSON.parse(j);
alert(o.acct_id+o.acct_name+o.email);
var jj='[{"acct_id":100000,"acct_name":"name0","email":"Jemail0"}]';
var oo=[];
oo=JSON.parse(jj);
alert(oo[0].acct_id+oo[0].acct_name+oo[0].email);
//------
function getJsonObjLength(jsonObj){
var l=0;for(var i in jsonObj){l++;}return l;
}
var myObj={"A0E":{"1":1}};
for(x in myObj){
console.log(x);//A0E
}
console.log(getJsonObjLength(myObj));//1
for(x in myObj){
console.log(myObj[x]);//Object{1:1}
}
console.log(myObj["A0E"]);//Object{1:1}
var obj =new Object();
obj.code="001";obj.name="hello";
var str=JSON.stringify(obj);
console.log(str);//{"code":"001","name":"hello"}
var jsonArr=new Array();
for(var i=0;i<2;i++){
var obj=new Object();
obj.title="title"+i;
obj.value=i;
jsonArr.push(obj);
}
var str=JSON.stringify(jsonArr);
console.log(str);//[{"title":"title0","value":0},{"title":"title1","value":1}]
var jj='[{"acct_id":100000,"acct_name":"name0","email":"Jemail0"}]';
var jStr1='{"code":"001"}';
jStr1="{\"code\":\"001\"}";
var jO=JSON.parse(jStr1);
console.log(jO["code"]); // 001
</script>
Exception: SyntaxError:Invalid
The name of the name/value pairs in JSON must be wrapped in double quotes. If it were otherwise, JSON.parse will throw a "sytaxError:Invalid" exception.
<script type="text/javascript">
try{
var j='{acct_id:100001,"acct_name":"na\\"me","email":"Jemail"}';//acct_id is not in double quotes.
var o=JSON.parse(j);//will throw an exception
alert(o.acct_id+o.acct_name+o.email);
}catch(e){
var j='{acct_id:100001,"acct_name":"na\\"me","email":"Jemail"}';
var o;
eval("o="+j);
alert("In Exception: \r\n"+o.acct_id+o.acct_name+o.email);
}
</script>
Encode and Decode
A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.
In ECMA262 version 5
Javascript Object to JSON | JSON to Javascript Object |
JSON.stringify() | JSON.parse() |
var j='{"acct_id":100001,"acct_name":"na\\"me","email":"Jet\'mail"}';
var o=JSON.parse(j);
alert(o.acct_id+o.acct_name+o.email);
var jj='[{"acct_id":100000,"acct_name":"name0","email":"Jemail0"}]';
var oo=[];
oo=JSON.parse(jj);
alert(oo[0].acct_id+oo[0].acct_name+oo[0].email);
//------
function getJsonObjLength(jsonObj){
var l=0;for(var i in jsonObj){l++;}return l;
}
var myObj={"A0E":{"1":1}};
for(x in myObj){
console.log(x);//A0E
}
console.log(getJsonObjLength(myObj));//1
for(x in myObj){
console.log(myObj[x]);//Object{1:1}
}
console.log(myObj["A0E"]);//Object{1:1}
var obj =new Object();
obj.code="001";obj.name="hello";
var str=JSON.stringify(obj);
console.log(str);//{"code":"001","name":"hello"}
var jsonArr=new Array();
for(var i=0;i<2;i++){
var obj=new Object();
obj.title="title"+i;
obj.value=i;
jsonArr.push(obj);
}
var str=JSON.stringify(jsonArr);
console.log(str);//[{"title":"title0","value":0},{"title":"title1","value":1}]
var jj='[{"acct_id":100000,"acct_name":"name0","email":"Jemail0"}]';
var jStr1='{"code":"001"}';
jStr1="{\"code\":\"001\"}";
var jO=JSON.parse(jStr1);
console.log(jO["code"]); // 001
</script>
Exception: SyntaxError:Invalid
The name of the name/value pairs in JSON must be wrapped in double quotes. If it were otherwise, JSON.parse will throw a "sytaxError:Invalid" exception.
<script type="text/javascript">
try{
var j='{acct_id:100001,"acct_name":"na\\"me","email":"Jemail"}';//acct_id is not in double quotes.
var o=JSON.parse(j);//will throw an exception
alert(o.acct_id+o.acct_name+o.email);
}catch(e){
var j='{acct_id:100001,"acct_name":"na\\"me","email":"Jemail"}';
var o;
eval("o="+j);
alert("In Exception: \r\n"+o.acct_id+o.acct_name+o.email);
}
</script>
Encode and Decode
Encode | Decode | unEncode | Unencode total |
encodeURIComponent :- >UTF-8 or $.param() in jQuery https://api.jquery.com/jQuery.param/ | decodeURIComponent | !, ‘,(,),*,-,.,_,~,0-9,a-z,A-Z | 71 |
encodeURI :- > UTF-8 | decodeURI | !,#,$,&,’,(,),*,+,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z | 82 |
escape :- > Unicode | unescape | *,+,-,.,/,@,_,0-9,a-z,A-Z | 69 |
评论
发表评论