javascript JSON

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 <jsonObj2.length; i++) {
  console.log(jsonObj2[i].FDEPTNAME);
}

    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 <jsonObj2.length; i++) {
    console.log(jsonObj2[i].FDEPTNAME);
}

    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 <jsonObj.length; i++) {
    console.log(jsonObj[i].FZJM);
}

    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 <jsonObj.length; i++) {
    console.log(jsonObj[i].FZJM);
}

    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}"

2023年1月31日15:23:23

JSON字符串转JSON对象

$.parseJSON(jsonstr); //jQuery.parseJSON(jsonstr))
JSON.parse(jsonstr);
eval('(' + jsonstr + ')');

JSON对象转JSON字符串

JSON.stringify(jsonobj);

 

写在最后

  哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!

 相关推荐:

posted @ 2017-05-16 10:24  Marydon  阅读(475)  评论(0编辑  收藏  举报