• 前端Json 增加,删除,修改元素(包含json数组处理)


    一:基础JSON对象

     二:JSON数组数据

     以下为增删修改方法:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Json</title>
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <script src="~/js/mui.min.js"></script>
        <link rel="stylesheet" type="text/css" href="~/css/reset.css">
        <script src="~/js/jquery1.7.2.js"></script> 
    </head>
    <body>
       <h2>清浅</h2>
    </body>
    </html>
    
    <Script>
    
    
        $(function () {
            //JsonDelete();
            //JsonAdd();
            //JsonArryDelete();
            JsonArryAdd();
        });
    
        //json对象删除数据
        function JsonDelete()
        {
            var jsonstr = '{"name":"张大佛爷", "Age":"170", "sex":"男" }';
            console.log(jsonstr);
    
            var json = JSON.parse(jsonstr);
            delete json.sex;
            console.log(json);
    
            jsonstr = JSON.stringify(json);
            console.log(jsonstr);
        }
    
        //json对象添加数据
        function JsonAdd()
        {
            var jsonstr = '{"name":"张大佛爷", "Age":"170" }';
            console.log(jsonstr);
    
            var json = JSON.parse(jsonstr);
            json.sex = '';
            console.log(json);
    
            jsonstr = JSON.stringify(json);
            console.log(jsonstr);
        }
    
        //Json数组删除
        function JsonArryDelete()
        {
            var jsonstr = '{"zhang":[{"name":"张大佛爷","Age":"170","sex":"男"},{"name":"张启灵","Age":"100","sex":"男"}],"wu":[{"name":"吴邪","Age":"25","sex":"男"}]}';
    
            var json = JSON.parse(jsonstr);        
            console.log(json);
    
            //删除张起灵        
            delete json.zhang[1];
            console.log(json);
    
            //强迫症,去掉null
            var zhangstr = [];
            for (var i = 0; i < json.zhang.length; i++) {
                var obj = json.zhang[i];
                if (obj != null)
                    zhangstr.push(obj);
            }
    
            json.zhang = zhangstr;
    
            jsonstr = JSON.stringify(json);
            console.log(jsonstr);        
        }
    
        //Json数组添加
        function JsonArryAdd() {
            var jsonstr = '{"zhang":[{"name":"张大佛爷","Age":"170","sex":"男"},{"name":"张启灵","Age":"100","sex":"男"}],"wu":[{"name":"吴邪","Age":"25","sex":"男"}]}';
    
            var json = JSON.parse(jsonstr);     
            console.log(json);
    
            var people = {
                "name": "吴三省",
                "Age": "45",
                "sex": ""
            }
    
            json.wu.push(people);
            console.log(json);
    
            jsonstr = JSON.stringify(json);
            console.log(jsonstr);
        }
    
    
    </Script>
     十年一瞬如沧海,谁人还逝藏海花 。  ——篮场滑行者·库里
  • 相关阅读:
    Linux 配置中文环境
    CookieContainer 丢失Cookie
    h5调用摄像头
    网络编程之Reactor 模式
    图形化命令行工具:
    关于VSTO调用Excel后进程无法退出的解决方案:
    ActionLink()与jquery更好地结合建造MVC网页:
    实现一个特殊栈,在实现栈的基本功能的基础上,再实现返回栈中最小元素的操作
    用数组结构实现大小固定的栈和队列
    比较器整理
  • 原文地址:https://www.cnblogs.com/cr-cool/p/11885978.html
Copyright © 2020-2023  润新知