博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript JSON
阅读量:6705 次
发布时间:2019-06-25

本文共 8028 字,大约阅读时间需要 26 分钟。

 

CreateTime--2016年10月12日09:28:09

迁移时间--2017年5月16日10:19:12

Author:Marydon

二、javascript-JSON

  说明:本文主要讲的是在javascript中对JSON数据的操作,若想看java中如何对JSON对象进行操作,请见数据格式汇总2-java文章

  (一)格式介绍

    标准格式:

    {"键":值,"键":值,"键":值}(值可以是数字,不加双引号;也可以是字符串,需加双引号;值也可以包含一个对象)

    格式一:值存储的是多个对象

{"data":[{"TERM":"第一期","WANGWU":199,"LISI":229,"ZHAOLIU":233,"SANQI":234,"ZHANGSAN":233},{"TERM":"第二期","WANGWU":156,"LISI":186,"ZHAOLIU":179,"SANQI":206,"ZHANGSAN":234},{"TERM":"第三期","WANGWU":133,"LISI":161,"ZHAOLIU":246,"SANQI":185,"ZHANGSAN":240},{"TERM":"第四期","WANGWU":194,"LISI":126,"ZHAOLIU":190,"SANQI":207,"ZHANGSAN":229},{"TERM":"第五期","WANGWU":149,"LISI":166,"ZHAOLIU":209,"SANQI":174,"ZHANGSAN":197}],"code":0,"expMsg":"","msg":"操作成功"}

    格式二:多个对象组成的数组

var row = '[{"TERM":"第一期","WANGWU":199,"LISI":229,"ZHAOLIU":233,"SANQI":234,"ZHANGSAN":233},{"TERM":"第二期","WANGWU":156,"LISI":186,"ZHAOLIU":179,"SANQI":206,"ZHANGSAN":234},{"TERM":"第三期","WANGWU":133,"LISI":161,"ZHAOLIU":246,"SANQI":185,"ZHANGSAN":240},{"TERM":"第四期","WANGWU":194,"LISI":126,"ZHAOLIU":190,"SANQI":207,"ZHANGSAN":229},{"TERM":"第五期","WANGWU":149,"LISI":166,"ZHAOLIU":209,"SANQI":174,"ZHANGSAN":197}]';

 (二)操作JSON

  2.2.1 json赋值

    在js中,[]表示数组;{}表示Json对象。

     以将map形式的字符串转成JSON对象为例举例

    方式一:声明JSON对象

/** * 将map形式的字符串转成JSON对象 * @param {string} mapStr * java-Map格式字符串 */function mapstringToJSON (mapStr) {    // 1.去除字符串中所有的"    mapStr = mapStr.replace(/"/g, "");    // 2.去除掉"{}"    mapStr = mapStr.substring(1, mapStr.length - 1);    // 3.将map字符串用逗号拆分成数组    var strs = mapStr.split(",");    // 声明一个长度为2的数组    var keyValue = new Array(2);    // JSON-键        var key = "";    // JSON-值    var value = "";    // 声明一个JSON对象    var json = {};    // 4.迭代map键值对    $(strs).each(function(index, str) {        keyValue = str.split("=");//按等号拆分成数组        key = $.trim(keyValue[0]);        value = $.trim(keyValue[1]);        json[key] = value;// 动态值        //json.key死值    });        return json;}var mapString = "{home=index, ForganizeCode=0, theme=window7, userAreaId=215, userManOrgs=10}";var jsonObj = mapstringToJSON(mapString);for (var key in jsonObj) {    console.log(jsonObj[key]);}

     注意:说说JSON对象中的"."与"[]"操作符的区别:

      上面的key是动态的值,若用点操作,即json.key,其结果就是json对象中有且只有一个键:key,其他的键都没有加上;

      赋值上:点操作指定的是死键,[]操作既可用作表示死键,又可用作表示动态的键。

      取值上:没太大区别。

    方式二:字符串拼接

/** * 将map形式的字符串转成JSON对象 * @param {string} mapStr * java-Map格式字符串 */function mapstringToJSON (mapStr) {    // 1.去除字符串中所有的"    mapStr = mapStr.replace(/"/g, "");    // 2.去除掉"{}"    mapStr = mapStr.substring(1, mapStr.length - 1);    // 3.将map字符串用逗号拆分成数组    var strs = mapStr.split(",");    //JSON字符串    var jsonStr = "";    //声明一个长度为2的数组    var keyValue = new Array(2);    //JSON-键    var key = "";    //JSON-值    var value = "";    // 4.迭代map键值对    $(strs).each(function(index, str) {        keyValue = str.split("=");//按等号拆分成数组        key = '"' + keyValue[0].trim() + '"';        value = '"' + keyValue[1].trim() + '"';        jsonStr += key + ":" + value + ",";//标准的JSON拼接格式    });    // 5.去除最后一个逗号并拼接"{}"    jsonStr = "{" + jsonStr.substring(0, jsonStr.length - 1) + "}";    // 6.json字符串转成JSON对象    var json = eval('(' + jsonStr + ')');    return json;}var mapString = "{home=index, ForganizeCode=0, theme=window7, userAreaId=215, userManOrgs=10}";var jsonObj = mapstringToJSON(mapString);for (var key in jsonObj) {    console.log(jsonObj[key]);}

    注意: 

       JSON字符串拼接原则:

        1.键必须有双引号,不能是单引号;

        2.键值对之间用逗号隔开;

        3.键与值之间使用冒号。

   2.2.2 json格式取值

   UpdateTime--2016年10月25日11:37:09

    2.2.2.1 json格式的字符串取值

    数据格式

var jsonStr = '{"data":[{"FSTATE":"1","FRCODE":"410000","FZJM":"YFBJK","ROWNO":"1","FREMARK":"","FDEPTNAME":"预防保健科","FPLVCODE":"","FDEPTCODE":"01"},{"FSTATE":"1","FRCODE":"410000","FZJM":"QKYLK","ROWNO":"2","FREMARK":"","FDEPTNAME":"全科医疗科","FPLVCODE":"","FDEPTCODE":"02"},{"FSTATE":"1","FRCODE":"410000","FZJM":"NK","ROWNO":"3","FREMARK":"","FDEPTNAME":"内科","FPLVCODE":"","FDEPTCODE":"03"},{"FSTATE":"1","FRCODE":"410000","FZJM":"WK","ROWNO":"4","FREMARK":"","FDEPTNAME":"外科","FPLVCODE":"","FDEPTCODE":"04"},{"FSTATE":"1","FRCODE":"410000","FZJM":"FCK","ROWNO":"5","FREMARK":"","FDEPTNAME":"妇产科","FPLVCODE":"","FDEPTCODE":"05"}],"code":0,"msg":"操作成功!","expMsg":""}';

    取值方法

//取对象var jsonObj = eval('('+jsonStr+')');console.log(jsonObj.msg);//取数组元素var jsonObj = eval('('+jsonStr+')');var jsonObj2 = jsonObj.data;for (var i = 0; i 

    2.2.2.2 json格式取值

    数据格式

var jsonStr = {"data":[{"FSTATE":"1","FRCODE":"410000","FZJM":"YFBJK","ROWNO":"1","FREMARK":"","FDEPTNAME":"预防保健科","FPLVCODE":"","FDEPTCODE":"01"},{"FSTATE":"1","FRCODE":"410000","FZJM":"QKYLK","ROWNO":"2","FREMARK":"","FDEPTNAME":"全科医疗科","FPLVCODE":"","FDEPTCODE":"02"},{"FSTATE":"1","FRCODE":"410000","FZJM":"NK","ROWNO":"3","FREMARK":"","FDEPTNAME":"内科","FPLVCODE":"","FDEPTCODE":"03"},{"FSTATE":"1","FRCODE":"410000","FZJM":"WK","ROWNO":"4","FREMARK":"","FDEPTNAME":"外科","FPLVCODE":"","FDEPTCODE":"04"},{"FSTATE":"1","FRCODE":"410000","FZJM":"FCK","ROWNO":"5","FREMARK":"","FDEPTNAME":"妇产科","FPLVCODE":"","FDEPTCODE":"05"}],"code":0,"msg":"操作成功!","expMsg":""};

    取值方法 

//取对象console.log(jsonStr.msg);//取数组元素var jsonObj2 = jsonStr.data;for (var i = 0; i 

    2.2.2.3 json数组字符串格式的取值

    数据格式  

var jsonStr = '[{"FSTATE":"1","FRCODE":"410000","FZJM":"YFBJK","ROWNO":"1","FREMARK":"","FDEPTNAME":"预防保健科","FPLVCODE":"","FDEPTCODE":"01"},{"FSTATE":"1","FRCODE":"410000","FZJM":"QKYLK","ROWNO":"2","FREMARK":"","FDEPTNAME":"全科医疗科","FPLVCODE":"","FDEPTCODE":"02"},{"FSTATE":"1","FRCODE":"410000","FZJM":"NK","ROWNO":"3","FREMARK":"","FDEPTNAME":"内科","FPLVCODE":"","FDEPTCODE":"03"},{"FSTATE":"1","FRCODE":"410000","FZJM":"WK","ROWNO":"4","FREMARK":"","FDEPTNAME":"外科","FPLVCODE":"","FDEPTCODE":"04"},{"FSTATE":"1","FRCODE":"410000","FZJM":"FCK","ROWNO":"5","FREMARK":"","FDEPTNAME":"妇产科","FPLVCODE":"","FDEPTCODE":"05"}]';

    取值方法

//取数组元素var jsonObj = eval('('+jsonStr+')');for (var i = 0; i 

    2.2.2.4 json格式数组的取值

    数据格式  

var jsonObj = [{"FSTATE":"1","FRCODE":"410000","FZJM":"YFBJK","ROWNO":"1","FREMARK":"","FDEPTNAME":"预防保健科","FPLVCODE":"","FDEPTCODE":"01"},{"FSTATE":"1","FRCODE":"410000","FZJM":"QKYLK","ROWNO":"2","FREMARK":"","FDEPTNAME":"全科医疗科","FPLVCODE":"","FDEPTCODE":"02"},{"FSTATE":"1","FRCODE":"410000","FZJM":"NK","ROWNO":"3","FREMARK":"","FDEPTNAME":"内科","FPLVCODE":"","FDEPTCODE":"03"},{"FSTATE":"1","FRCODE":"410000","FZJM":"WK","ROWNO":"4","FREMARK":"","FDEPTNAME":"外科","FPLVCODE":"","FDEPTCODE":"04"},{"FSTATE":"1","FRCODE":"410000","FZJM":"FCK","ROWNO":"5","FREMARK":"","FDEPTNAME":"妇产科","FPLVCODE":"","FDEPTCODE":"05"}];

    取值方法

//取数组元素for (var i = 0; i 

    2.2.2.5 判断JSON对象是否为空

    jQuery.isEmptyObject(JSONObj)

  (三)格式转换

    3.3.1 json格式字符串与JSON对象的相互转化

      3.3.1.1 json格式字符串转换成JSON对象  

      方法:使用eval('(' + 字符串 + ')') 

         json格式字符串转换成object类型的数组(为了便于理解,可以看成java中的JSONArray)

var jsonArray = eval("("+row+")");console.log(jsonArray);//"[object Object],[object Object],[object Object],[object Object],[object Object]"

      3.3.1.2 object类型(java中的JSONArray)转化成json字符串

      方法:使用JSON.stringify() 

var str = JSON.stringify(jsonArray);console.log(str);//"[{"TERM":"第一期","WANGWU":199,"LISI":229,"ZHAOLIU":233,"SANQI":234,"ZHANGSAN":233},{"TERM":"第二期","WANGWU":156,"LISI":186,"ZHAOLIU":179,"SANQI":206,"ZHANGSAN":234},{"TERM":"第三期","WANGWU":133,"LISI":161,"ZHAOLIU":246,"SANQI":185,"ZHANGSAN":240},{"TERM":"第四期","WANGWU":194,"LISI":126,"ZHAOLIU":190,"SANQI":207,"ZHANGSAN":229},{"TERM":"第五期","WANGWU":149,"LISI":166,"ZHAOLIU":209,"SANQI":174,"ZHANGSAN":197}]"

    3.3.2 JSON对象转换成map字符串

/** * 将JSON对象转换成map形式的字符串 * @param {Object} jsonObj  *     JSON对象 */function JSONToMapstring (jsonObj) {    var mapString = "";    //遍历JSON对象    for (var item in jsonObj) {        mapString += item + "=" + jsonObj[item] + ",";//标准的java—Map字符串拼接格式    }    //去除最后一个逗号并转成map形式字符串    mapString = "{" + mapString.substring(0, mapString.length - 1) + "}";    return mapString;}

      举例:

//接着上面的例子var result = JSONToMapstring(jsonObj);console.log(result);//"{home=index,ForganizeCode=0,theme=window7,userAreaId=215,userManOrgs=10}"

 

 

 

转载地址:http://lsflo.baihongyu.com/

你可能感兴趣的文章
设置Android软键盘的默认不弹出
查看>>
面向对象思想
查看>>
Logback配置详解
查看>>
快速安装git-gitbook(python升级)
查看>>
《获取java项目根目录 》
查看>>
Java零散知识点
查看>>
Windows下 eclipse的相关操作(一):下载安装
查看>>
iOS的socket开发基础
查看>>
Intent详解
查看>>
myeclipse 中的 Axis2 的使用
查看>>
nginx_(1)
查看>>
各种SKYPE网页代码,SKYPE在线代码
查看>>
你了解JavaScript中的function吗?
查看>>
网页制作重点记录
查看>>
水平分割
查看>>
写给高端大气上档次的你!
查看>>
J2SE 5.0新的特性——Annotation(注释)
查看>>
Crontab 用法
查看>>
VMware ESXi linux虚拟机在线识别新添加磁盘
查看>>
我的友情链接
查看>>