前述:终于抽出时间,整理了一些编程中所需基础知识。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>json基础</title>
</head>
<body>
<script type="text/javascript">
    console.log('第一章:json遍历及取值');
    console.log("案例一:");
    var jsonstr = '[{"id":"ffsde42jj4k2f33gh2ew4eef34545j","ProjectName":"北京xxx招标项目"},' +
        '{"id":"fhghrree42jj4k2f33gh2ew4eef34545j","ProjectName":"上海xxxx1标造价项目"},' +
        '{"id":"fhhss2jj4k2f33gh2ew4eef34545j","ProjectName":"天津xxx机电xxx"}]';
    var jsondata = new Function("return" + jsonstr)();//json字符串转json对象
    for (i = 0; i < jsondata.length; i++) {
        for (values in jsondata[i]) {
            console.log(jsondata[i][values]);
        }
    }
    console.log("案例二:");
    var jsondatas = eval(jsonstr);//json字符串转json对象
    for (i = 0; i < jsondatas.length; i++) {
        console.log("id:" + jsondatas[i]['id'] + ";");
        console.log("ProjectName:" + jsondatas[i]['ProjectName'] + "。");
    }
    console.log('案例三:');
    var jsonstrtwo = '{"id":"ffsde42jj4k2f33gh2ew4eef34545j","ProjectName":"北京xxx招标项目"}';
    var jsonobj = JSON.parse(jsonstrtwo);//json字符串转json对象
    console.log("id:" + jsonobj['id'] + "。");
    console.log("ProjectName:" + jsonobj["ProjectName"] + "。");
    console.log('案例四:');
    var json = [{"id": "ffsde42jj4k2f33gh2ew4eef34545j", "ProjectName": "北京xxx招标项目"},
        {"id": "fhghrree42jj4k2f33gh2ew4eef34545j", "ProjectName": "上海xxxx1标造价项目"},
        {"id": "fhhss2jj4k2f33gh2ew4eef34545j", "ProjectName": "天津xxx机电xxx"}];//json对象
    for (i in json) {
        console.log(json[i]);
        console.log("id:" + json[i].id + ", ProjectName:" + json[i].ProjectName + "。");
    }
    console.log('案例五:');
    var jsonobjectAll = {
        "info": "[{'id':'ffsde42jj4k2f33gh2ew4eef34545j','ProjectName':'北京xxx招标项目'}," +
            "{'id':'fhghrree42jj4k2f33gh2ew4eef34545j','ProjectName':'上海xxxx1标造价项目'}," +
            "{'id':'fhhss2jj4k2f33gh2ew4eef34545j','ProjectName':'天津xxx机电xxx'}]"
    };//一组json对象
    console.log(jsonobjectAll);
    var jsonobj = eval(jsonobjectAll.info);//转多组json对象
    console.log(jsonobj);
    for (var i = 0; i < jsonobj.length; i++) {
        console.log("id:" + jsonobj[i].id + ",ProjectName: " + jsonobj[i].ProjectName);
    }
    console.log('案例六:');
    console.log('请看:《ajax读取json数据》《js读取本地json文件》及浏览器跨域设置 https://blog.csdn.net/xysxlgq/article/details/119568570 ');
    console.log('案例七:');
    console.log('中国城市列表选择:请看《js基础-中国城市筛选》https://blog.csdn.net/xysxlgq/article/details/119582062');
    console.log('第二章:json转换及修改');//测试其中一个请先注释掉其他三个。

    console.log('(1):添加json对象');
    var newjson={
        "id":"kweokr345lsd4k2jk4k532ll4",
        "ProjectName":"太原xxxx工程xxxx"
    };
    jsonobj.push(newjson);
    console.log(jsonobj);

    console.log('(2)添加属性并赋值 :');
    jsonobj[2].projecttype="BIM";
    console.log(jsonobj);

    console.log('(3):属性替换值。');
    jsonobj[2].ProjectName="西安xxxx工程xxxx";
    console.log(jsonobj);

    console.log('(4)属性删除值');
    delete jsonobj[2].ProjectName;
    console.log(jsonobj);
    //未完待续...
</script>
</body>
</html>

GitHub 加速计划 / js / json
17
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:2 个月前 )
960b763e 5 个月前
8c391e04 8 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐