返回的数据处理—字符串切割成对象

  1. //参数切割
  2. //id=811bf077d20c4d73a6c01334db8cfdd9&cur=1&limit=15--完成前,字符串
  3. //{id: "811bf077d20c4d73a6c01334db8cfdd9", cur: "1", limit: "15"} --完成后,对象
  4. function getParam(param){
  5. var theParam = new Object();
  6. var params = param.split("&");
  7. for(var i=0;i<params.length;i++){
  8. theParam[params[i].split('=')[0]] = params[i].split('=')[1]
  9. }
  10. return theParam;
  11. }

返回的数据处理—对象拼接成字符串

  1. //参数拼接
  2. //{id: "6e2dfd6235f240d0aa00ab1c6530dd76", userName: "用户18819211909"} --完成前,对象
  3. //id=6e2dfd6235f240d0aa00ab1c6530dd76&userName=用户18819211909 --完成后,字符串
  4. function setParam(param){
  5. var listparams='';
  6. for(var key in param){
  7. listparams+='&'+key+'='+param[key];
  8. }
  9. listparams=listparams.substring(1);
  10. return listparams;
  11. }

字符串对象转成真正的对象

  1. //字符串对象转对象"{TYPE=特邀名家, PROFITRATE=0, HITRATE=0, INTRODUCE=123,}"转成"{TYPE:"特邀名家", PROFITRATE:"0", HITRATE="0", INTRODUCE="123"}"
  2. function changeStringToObj(data){
  3. var data_list = data.split(',');
  4. var obj={},data_array='';
  5. for(var j=0;j< data_list.length;j++){
  6. data_array = data_list[j].split('=');
  7. obj[data_array[0].replace(/\s|\{/g,'')] = data_array[1] //正则匹配去掉空格和大括号
  8. }
  9. return obj
  10. };

字符串对象切割出里面的数组中的前三位

  1. //当datas为'{ODDS=["152.5","0.85","0.99;151.5","0.75","1.09;153.5","0.95","0.89"], SCOREONE=0}'字符串形式时切割出ODDS数组中的前三位数152.5,0.85,0.99
  2. function getOddsAndRqs(data,index){
  3. var stringSplit='';
  4. var strings = data.slice(data.indexOf(index)+5,data.indexOf(index)+60); //60是预估的数,没有实际参考得出
  5. strings = strings.replace(/\[|]|"/g, "");
  6. if(strings.indexOf(';') !=0){
  7. strings = strings.split(';');
  8. stringSplit = strings[0].split(',');
  9. }else{
  10. stringSplit = strings.split(',')
  11. }
  12. return stringSplit
  13. }

字符串对象切割出需要的数据

  1. //当data为'{BIG=, OU=1.75,3.10,4.33, YA=, MATCHID=1787181}'字符串形式时切割出OU=1.75,3.10,4.33,type为了区分类型
  2. function sub_string(data,type){
  3. //console.log(data)
  4. var datas_list = data.split(' ');
  5. var datas_str='',datas_string='';
  6. for(var j=0;j< datas_list.length;j++){
  7. datas_str = datas_list[j].replace('{','').replace('}','').replace(' ','');
  8. datas_string = datas_str.split('=');
  9. if(type == datas_string[0]){
  10. if(datas_string[1].length > 1){
  11. return datas_string[1].split(',')
  12. }else{
  13. return ["0","0","0"]
  14. }
  15. }
  16. }
  17. }