首页 > 代码库 > 数据格式汇总-js
数据格式汇总-js
CreateTime--2016年10月12日09:28:09
Author:Marydon
数据格式汇总
一、数据格式介绍
标准的json格式--{"键":值,"键":值,"键":值}(值可以是数字,不加双引号;也可以是字符串,需加双引号;值也可以包含一个对象)
格式一:值存储的是多个对象
{"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}]‘;
赋值给数组
var xAxisValues = [];//X轴坐标值var jsonObj = eval("("+row+")");//转换成了由Object类型的元素组成的数组/* 这种方式不行,foreach只能遍历list集合,不能用于迭代数组 * $(jsonObj).foreach(function(index,value) {* shuzu.push(value.TERM);* }); *//* 也不能使用这种迭代方式(js不支持这种迭代方式)* for (Object json:jsonObj) {* shuzu.push(json.TERM);* } *//*遍历数组只能使用for循环*/for (var i = 0; i <jsonObj.length; i++) { shuzu.push(jsonObj[i].TERM);}console.log(shuzu);//["第一期", "第二期", "第三期", "第四期", "第五期"]
UpdateTime--2016年10月25日11:37:09
json格式取值问题
a.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);}
b.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);//取数组元素//console.log(jsonStr.data);不能这样var jsonObj2 = eval(jsonStr.data);for (var i = 0; i <jsonObj2.length; i++) { console.log(jsonObj2[i].FDEPTNAME);}
c.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);}
d.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);}
小结:
如果是json格式字符串,取值用eval(‘(‘+json格式字符串+‘)‘);
如果是json格式数据,取值用eval(json格式);
java-Map格式
{data=http://www.mamicode.com/[{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=操作成功}
/** * 在js中使用EL表达式获取后台返回的数据 */var row = ‘${model.data}‘;
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}]
/**不能用for循环 * for (var i =0; i<row.length; i++) { * xAxisValues.push(‘${row[i].TERM}‘); * } */只能用 jstl标签库的c标签迭代 <c:forEach var="row" items="${model.data}"> xAxisValues.push(‘${row.TERM}‘); </c:forEach> console.log(xAxisValues);
/* * 在js中也可以使用小脚本获取后台返回的数据 *///var test = ‘<%=request.getAttribute("model")%>‘;//{data=http://www.mamicode.com/[{TERM=第二期, WANGWU=156, LISI=186, ZHAOLIU=179, SANQI=206, ZHANGSAN=234}, {TERM=第三期, WANGWU=133, LISI=161, ZHAOLIU=246, SANQI=185, ZHANGSAN=240}, {TERM=第五期, WANGWU=149, LISI=166, ZHAOLIU=209, SANQI=174, ZHANGSAN=197}, {TERM=第一期, WANGWU=199, LISI=229, ZHAOLIU=233, SANQI=234, ZHANGSAN=233}, {TERM=第四期, WANGWU=194, LISI=126, ZHAOLIU=190, SANQI=207, ZHANGSAN=229}], code=0, expMsg=, msg=操作成功}
二、数据格式间的相互转化
在js中,json格式的字符串与Object类型可以相互转化
a.json格式字符串转换成Object类型的数组
var jsonObj = eval("("+row+")");console.log(jsonObj);//"[object Object],[object Object],[object Object],[object Object],[object Object]"
b.Object类型转化成json字符串
var str = JSON.stringify(jsonObj);console.log(str);//"[{"TERM":"第一期","WANGWU":199,"LISI":229,"ZHAOLIU":233,"SANQI":234,"ZHANGSAN":233},{"TERM":"第\t\t 二期","WANGWU":156,"LISI":186,"ZHAOLIU":179,"SANQI":206,"ZHANGSAN":234},{"TERM":"第三\t\t 期","WANGWU":133,"LISI":161,"ZHAOLIU":246,"SANQI":185,"ZHANGSAN":240},{"TERM":"第四\t\t\t 期","WANGWU":194,"LISI":126,"ZHAOLIU":190,"SANQI":207,"ZHANGSAN":229},{"TERM":"第五\t\t\t 期","WANGWU":149,"LISI":166,"ZHAOLIU":209,"SANQI":174,"ZHANGSAN":197}]"
数据格式汇总-js
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。