javascript - 在 jQuery 中制作 JSON 对象的最佳方法是什么?

标签 javascript jquery ajax json

在 jQuery 中创建 JSON 对象的最佳方法是什么(不使用解析器或 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 是 JavaScript 对象字面量表示法的一个子集。由于 JSON 是 JavaScript 的一个子集,因此它可以毫不费力地在该语言中使用。

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

source

但是,要从外部源解析 JSON 或从您自己的代码序列化 JSON 对象,您需要一个库,例如 JSON-js因为 Javascript/ECMAScript 目前不支持这个,尽管:

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

关于javascript - 在 jQuery 中制作 JSON 对象的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6404994/

相关文章:

javascript - Angular 5 Material 设计全 Angular 输入

javascript - 如何在 JavaScript 中按行空格/换行符拆分字符串

javascript - 淡入淡出在javascript中切换类

javascript - 连接数组会导致将它们作为对象连接

javascript - 遍历具有匹配数据属性值的元素

jquery - 使用 jQuery ajax 调用 Restful Web 服务

javascript - 创建具有随机颜色且不重叠的随机矩形

javascript - 如何将多个异步 $.get 请求的结果存储在一个数组中?

javascript - mvc $ ('form' ).submit 不在部分 View 上执行

javascript - 嵌套路由: How to query Rails DB and store values in JavaScript variables?