开发者

What is the best way to make a JSON object in jQuery?

开发者 https://www.devze.com 2023-03-14 11:44 出处:网络
开发者_运维技巧What is the best way to make a JSON object in jQuery (without using a parser or AJAX)?
开发者_运维技巧

What is the best way to make a JSON object in jQuery (without using a parser or AJAX)?

var JSONobj = new JSON({'a':'b'})


JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages...These properties make JSON an ideal data-interchange language.

source

JSON is a subset of the object literal notation of JavaScript. Since JSON is a subset of JavaScript, it can be used in the language with no muss or fuss.

var myJSONObject = {"bindings": [
        {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
        {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
        {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
    ]
};

source

However to parse JSON from an external source or serialize JSON objects from your own code, you'll need a library such as JSON-js as Javascript/ECMAScript doesn't currently support this, although:

It is expected that native JSON support will be included in the next ECMAScript standard.


JSON is the serialized representation of an object. It is just a string. To create a JSON representation out of a JavaScript object, use JSON.stringify.

var myObject = { hello: "world", foo: [ "bar", "baz", 42 ] };

JSON.stringify(myObject); // "{"hello":"world","foo":["bar","baz",42]}"


You should be able to just use an object literal syntax:

var JSONobj = {'a':'b'};


I'm not sure what you're trying to do, but if you just want to create an object, create it...

var myObj = { a : "b" };
0

精彩评论

暂无评论...
验证码 换一张
取 消