转自:

https://www.jianshu.com/p/6501b0f3124f

  1. 直接定义json
   var json = {"name": "小明", "age": 12};
console.log(json);
  1. json 转 String
    var str = JSON.stringify(json);
console.log(str);
  1. String 转 Json
json = JSON.parse(str);
console.log(json)
  1. 添加新的字段
  // 方式1
json.sex = '女';
// 方式2
var id = 'id';
json[id] = '123';
console.log(json)
  1. 判断字段是否存在
console.log(json.hasOwnProperty(id))
  1. 删除字段
方式1
delete json.id;
console.log(json);
方式2
delete json[id];
console.log(json);
  1. 添加JsonArray
var array = [{"name": "小李", "age": 20}];
console.log(array);
array.push(json);
console.log(array);
  1. 遍历JsonArray
for (var item in array) {
console.log(array[item].name)
}
  1. 删除array中的一项
 array.splice(1)

完整代码示例:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> <script type="text/javascript">
var json = {"name": "小明", "age": 12};
console.log(json);
var str = JSON.stringify(json);
console.log(str);
json.sex = '女';
var id = 'id';
json[id] = '123';
console.log(json)
console.log(json.hasOwnProperty(id))
delete json.id;
console.log(json);
id = 'id';
json[id] = '123';
console.log(json);
delete json[id];
console.log(json);
var array = [{"name": "小李", "age": 20}];
console.log(array);
array.push(json);
console.log(array);
for (var item in array) {
console.log(array[item].name)
}
array.splice(1)
console.log(array)
</script>
</body>
</html>
 
 

0人点赞

 

JS 中Json常用操作的更多相关文章

  1. js,jQuery数组常用操作小结

    一.js中数组常用操作小结 (1) shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift() ...

  2. js中JSON的解析(将json字符串转化为对象)和序列化(将对象转化为json字符串)(函数的功能一般都挺全的,需要的时候去查看完整函数)

    js中JSON的解析(将json字符串转化为对象)和序列化(将对象转化为json字符串)(函数的功能一般都挺全的,需要的时候去查看完整函数) 一.总结 1.JSON解析:JSON.parse(myJS ...

  3. javascript中字符串常用操作整理

    javascript中字符串常用操作整理 字符串的操作在js中非常频繁,也非常重要.以往看完书之后都能记得非常清楚,但稍微隔一段时间不用,便会忘得差不多,记性不好是硬伤啊...今天就对字符串的一些常用 ...

  4. js中的DOM操作汇总

    一.DOM创建 DOM节点(Node)通常对应于一个标签,一个文本,或者一个HTML属性.DOM节点有一个nodeType属性用来表示当前元素的类型,它是一个整数: Element,元素 Attrib ...

  5. js中Json字符串如何转成Json对象(4种转换方式)

    js中Json字符串如何转成Json对象(4种转换方式) 一.总结 一句话总结:原生方法(就是浏览器默认支持的方法) 浏览器支持的转换方式(Firefox,chrome,opera,safari,ie ...

  6. js中json数据简单处理(JSON.parse()和js中嵌套html)

    js中json数据简单处理(JSON.parse()和js中嵌套html) 一.总结 1.html中嵌套js:<script>js代码</script> 2.js中嵌套html ...

  7. js中json法创建对象(json里面的:相当于js里面的=)

    js中json法创建对象(json里面的:相当于js里面的=) 一.总结 json里面的:相当于js里面的= 4.json创建js对象解决命名冲突:多个人为同一个页面写js的话,命名冲突就有可能发生, ...

  8. js中 json对象与json字符串相互转换的几种方式

    以下总结js中 json对象与json字符串相互转换的几种方式: 一.JSON对象转化为JSON字符串 1.使用JSON.stringify()方法进行转换 该方法不支持较老版本的IE浏览器,比如:i ...

  9. JS中的常用的代码操作

    本文件介绍常用的js代码的DOM操作.CSS操作.对象(Object对象.Array对象.Number对象.String对象.Math对象.JSON对象和Console对象)操作说明. 一.DOM树的 ...

随机推荐

  1. ajax请求传base64太大,springboot后台无法接收

    ajax请求传base64太大,springboot后台无法接收 .具体体现形式:对应属性值为null 在 application.yml中添加: server: tomcat: max-http-p ...

  2. MySQL8.0项目启动遇到的问题

    写在前面 看到jeecg论坛更新了jeecg-boot版本, 比较新颖的技术都有, down代码, 执行sql脚本, 起项目, 本来是一气呵成的事儿遇到了两个问题, 做个记录. 环境: IDEA201 ...

  3. 005 vue路由

    一:元素的获取 1.ref元素获取 可以通过ref获取DOm,也可以获取组件的引用 <!DOCTYPE html> <html lang="en"> < ...

  4. shell关闭指定进程

    例如要关闭jupyter-notebook这个进程: ps -ef | grep jupyter-notebook | grep -v grep | cut -c 9-15 | xargs kill ...

  5. osg指定向量旋转指定角度

    向量AB,沿着n旋转10度 osg::Vec3 left = AB*osg::Matrix::rotate(osg::inDegrees(10), n); osg::Vec3 right = AB*o ...

  6. Ehcache API的使用和注意点

    目录 创建CacheManager CacheManager常用的API 创建Cache Cache常用的API 创建Element Element常用的API 配置文件 配置文件名为ehcache. ...

  7. [LeetCode] 68. Text Justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  8. [LeetCode] 815. Bus Routes 公交路线

    We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For e ...

  9. 从一个案例窥探ORACLE的PASSWORD_VERSIONS

    1.环境说明 ORACLE 客户端版本 11.2.0.1 ORACLE 服务端版本 12.2.0.1 2.异常现象 客户端(下文也称为Cp)访问服务端(Sp),报了一个错误: Figure 1 以错误 ...

  10. LeetCode 201. 数字范围按位与(Bitwise AND of Numbers Range)

    201. 数字范围按位与 201. Bitwise AND of Numbers Range 题目描述 给定范围 [m, n],其中 0 <= m <= n <= 214748364 ...