json与object
json与object的区别:
1、JSON是对象,但对象不一定是JSON
2、对象的组成是由属性和属性值,也就是KEY->VALUE对组成,value可是是任意的数据类型,当value为一个函数的时候,这个时候叫做方法。而你通过
JSON.parse()传入的字符串并里面有VALUE为function的吗?不可能像下面的例子一样写这个被转换的字符串:
var a ={name:"",say:function(){}};
JSON.parse(a);
以上代码,是没有办法执行的。即使是服务器转换的JSON字符串,也没有把方法转换进这个字符串的。
3、在JSON对象中出现的value始终都不可能是一个函数,如果转换后添加进去方法,那就变成一个真正的JS对象了。
4、JSON是一种数据结构,并不是对象。(所以没有方法)
(JSON和对象的区别就在于有没有方法,在JS中某种意义上讲JSON.parse()转换的就是一个对象。)
整理转自:https://ask.csdn.net/questions/661237
json方法:
JSON.parse()
解析JSON字符串并返回对应的值,可以额外传入一个转换函数,用来将生成的值和其属性,在返回之前进行某些修改
JSON.stringify()返回与指定对应的JSON字符串,可以通过额外的参数,控制仅包含某些属性,或者以自定义方法来替换某些key对应的属性值。
JSON.parse()- 解析JSON字符串并返回对应的值,可以额外传入一个转换函数,用来将生成的值和其属性, 在返回之前进行某些修改。
JSON.stringify()- 返回与指定值对应的JSON字符串,可以通过额外的参数, 控制仅包含某些属性, 或者以自定义方法来替换某些key对应的属性值。
Polyfill节
JSON对象可能不被老版本的浏览器支持。可以将下面的代码放到JS脚本最开始的位置,这样就可以在没有原生支持 JSON 对象的浏览器(如IE6)中使用 JSON对象。
以下算法是对原生JSON对象的模仿:
if (!window.JSON) {
window.JSON = {
parse: function(sJSON) { return eval('(' + sJSON + ')'); },
stringify: (function () {
var toString = Object.prototype.toString;
var isArray = Array.isArray || function (a) { return toString.call(a) === '[object Array]'; };
var escMap = {'"': '\\"', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t'};
var escFunc = function (m) { return escMap[m] || '\\u' + (m.charCodeAt(0) + 0x10000).toString(16).substr(1); };
var escRE = /[\\"\u0000-\u001F\u2028\u2029]/g;
return function stringify(value) {
if (value == null) {
return 'null';
} else if (typeof value === 'number') {
return isFinite(value) ? value.toString() : 'null';
} else if (typeof value === 'boolean') {
return value.toString();
} else if (typeof value === 'object') {
if (typeof value.toJSON === 'function') {
return stringify(value.toJSON());
} else if (isArray(value)) {
var res = '[';
for (var i = 0; i < value.length; i++)
res += (i ? ', ' : '') + stringify(value[i]);
return res + ']';
} else if (toString.call(value) === '[object Object]') {
var tmp = [];
for (var k in value) {
if (value.hasOwnProperty(k))
tmp.push(stringify(k) + ': ' + stringify(value[k]));
}
return '{' + tmp.join(', ') + '}';
}
}
return '"' + value.toString().replace(escRE, escFunc) + '"';
};
})()
};
}
业界更专业, 更强大的JSON对象 polyfills 是 JSON2 和 JSON3。
规范节
| 规范 | 状态 | 说明 |
|---|---|---|
| ECMAScript 5.1 (ECMA-262) JSON |
Standard | Initial definition. |
| ECMAScript 2015 (6th Edition, ECMA-262) JSON |
Standard | |
| ECMAScript Latest Draft (ECMA-262) JSON |
Draft |
浏览器兼容性节
| Desktop | Mobile | Server | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | Node.js | |
| Basic support | Full supportYes | Full supportYes | Full support3.5 | Full support8 | Full support10.5 | Full support4 | Full supportYes | Full supportYes | Full supportYes | Full support4 | Full supportYes | Full supportYes | Full supportYes | Full supportYes |
parse |
Full supportYes | Full supportYes | Full support3.5 | Full support8 | Full support10.5 | Full support4 | Full supportYes | Full supportYes | Full supportYes | Full support4 | Full supportYes | Full supportYes | Full supportYes | Full supportYes |
stringify |
Full supportYes | Full supportYes | Full support3.5 | Full support8 | Full support10.5 | Full support4 | Full supportYes | Full supportYes | Full supportYes | Full support4 | Full supportYes | Full supportYes | Full supportYes | Full supportYes |
| JavaScript is a superset of JSON | Full support66 | No supportNo | Full support62 | No supportNo | Full support53 | Full supportYes | Full support66 | Full support66 | No supportNo | Full support62 | Full support53 | Full supportYes | No supportNo | Full support10.0.0 |
Legend
- Full support
- Full support
- No support
- No support
相关链接节
- JavaScript 标准库
JSON- 方法
- 继承
Object- 属性
- 方法
Object.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toSource()Object.prototype.toString()Object.prototype.unwatch()Object.prototype.valueOf()Object.prototype.watch()Object.setPrototypeOf()
json与object的更多相关文章
- Conversion between json and object using SBJson lib
Define two methods in an object class as follows: @interface MyObject : NSObject @property (nonatomi ...
- JSON(JavaScript Object Notation, JS 对象标记)
JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.它基于 ECMAScript (w3c制定的js规范)的一个子集,采用完全独立于编程语言 ...
- C# Json To Object 无废话
json字符串如下: { success : 0, errorMsg : "错误消息", data : { total : "总记录数", rows : [ { ...
- JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式
JSON JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 它基于JavaScript(Standard ECMA-262 3rd Edition - D ...
- C# Net 通用json转Object(对象)
C# Net 通用 json 转 Object 对象 C# Net 提取 json 字符串 对象 数组 C# Net json 对象 中有字符串 转为 对象 例如输入:{"1" ...
- Java 用Jackson进行json和object之间的转换(并解决json中存在新增多余字段的问题)
1.添加jackson库 如果是maven工程,需要在pom.xml中添加jackson的依赖: <dependency> <groupId>com.fasterxm ...
- JS json对象(Object)和字符串(String)互转方法
[JS json对象(Object)和字符串(String)互转方法] 参考:https://blog.csdn.net/wenqianla2550/article/details/78232706 ...
- net.sf.json.JSONException: Object is null
出现这个错误的原因是net.sf.json.JSONArray或JSONObject转换时,对象内包含另一个对象,而该被包含的对象为NULL,所以抛出异常. 补充: 最可恨的是,明明转换的时候已经成功 ...
- Conversion between json and object
public static string ObjToJson<T>(T obj) { DataContractJsonSerializer serializer = new DataCon ...
随机推荐
- 利用junit对springMVC的Controller进行测试
本文转自http://www.tuicool.com/articles/7rMziy 平时对junit测试service/DAO层已经很熟悉不过了,如果不了解,可以猛戳这里,但是我们要测试contro ...
- 完毕port模型
完毕port模型过程例如以下: 1.调用CreateIoCompletionPort函数创建完毕port. HANDLE CompletionPort=CreateIoCompletionStatus ...
- SecureCRT 会话丢失的处理办法
SecureCRT 会话丢失的处理办法 在SecureCRT中已经有了70多个session,密码都记忆了,当然有些失效的也没有删除: 某一天,打开SecureCRT之后,发现session都没有了, ...
- C# - Generics
Generics were added to version 2.0 of the C# language and the common language runtime (CLR). Generic ...
- Android 源代码解析 之 setContentView
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41894125,本文出自:[张鸿洋的博客] 大家在平时的开发中.对于setCont ...
- iOS 获取当前时间格式化字符串
iOS 获取当前时间格式化字符串 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保 ...
- cmd执行调用打开文件
Process p = new Process();//新进程 p.StartInfo.FileName = "cmd.exe";//打开cmd程序 p.StartInfo.Use ...
- actionbarsherlock示例
package com.example.viewpagerandtabdemo; import java.util.ArrayList; import java.util.List; import a ...
- [JavaEE] Mybatis与Ibatis比较
随着开发团队转投Google Code旗下,ibatis3.x正式更名为Mybatis 虽然从正式版发布至今也有近一年时间,官方也非常友好的提供了中文版的使用手册,不过相信很多人还在项目中使用ibat ...
- 虚拟机CentOS设置IP
虚拟机里Centos7的IP地址查看方法 本地虚拟机安装了CentOS 7,想通过ftp上传文件,发现通过ifconfig,没有inet这个属性 查看ens33网卡的配置:vi /etc/syscon ...