What object/type does JSON support?

Source: Internet
Author: User

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?

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.