Original: What object/type does JSON support?
When an interviewer asks you: What object/type does JSON support? What's your answer?
Perhaps his intention is the following answer:
The JSON format supports the following data types:
type |
Description |
Number |
Double-precision floating-point format in JavaScript |
String |
Backslash-escaped Unicode for double quotes |
Boolean |
True or False |
Array |
Ordered sequence of values |
Value |
It can be a string, a number, true or False (True/false), empty (null), etc. |
Object |
Unordered collection key-value pairs |
Whitespace |
You can use any of the tokens in a pair |
Null |
Empty |
But I really don't think so, I think support arbitrary object type, as long as the receiving container exists in it.
You can use the following example to prove that:
<!DOCTYPE HTML><HTMLLang= "ZH-CN"><Head><MetaCharSet= "Utf-8"><Scriptsrc=".. /resources/js/jquery-1.8.3.min.js "></Script></Head><Body></Body></HTML><Script> functionClassA (scolor) { This. Color=Scolor; This. Saycolor= function() {alert ( This. color); }; } functionStrtojson (str) {varJSON=Eval ('(' +Str+ ')'); returnJSON; } jquery.ajax ({type:"Get", Cache:false, DataType:"text", URL:"Simple.json", Success:function(data) {alert (data); var_json=Strtojson (data); _json.testcolor.saycolor (); //This is the effect we want to see.}, Error:function() {alert ('Sorry, the service request is abnormal! '); } }); </Script>
Simple.json File Contents:
{ "retCode": "0000", "retmsg": "Success", new ClassA ("Red"), "Retlist": { "Le1": { "price": "4800000", "Commid": "56761 " }, " Le2 ": { " price ":" 4800000 ", " Commid ":" 56761 " } }}
Note the DataType in the above code: "Text"
Because the jquery source is converted in the following way, we need a more primitive way, so I replaced the custom Strtojson (str)
jquery Source code:
//evaluates a script in a global context //workarounds based on findings by Jim Driscoll //Http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-contextGlobaleval:function(data) {if(Data &&core_rnotwhite.test (data)) { //We use ExecScript on Internet Explorer //We Use a anonymous function So, context is window //rather than jQuery in Firefox(Window.execscript | |function(data) {window["Eval"].call (window, data); }) (data); } },
In fact, JSON is a string that requires a front-end eval transformation, so it's not easy to say what the JSON supports or what data types are supported.
Pay attention to the way of questioning, need more rigorous to ask the question we want to ask.
What object/type does JSON support?