数据格式汇总-js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据格式汇总-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=[{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=[{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的主要内容,如果未能解决你的问题,请参考以下文章

数据格式汇总2-java

Jquery插件 easyUI属性汇总

IDEA格式化代码技巧汇总

如何在 Reactjs 中添加丰富的代码片段?

JSON简介以及用法汇总

Relay.js 没有正确解析组合片段