c结构体赋值的另类操作 附带另类json解析的函数
typedef struct { int floor; //0 1 2 3 4 5.... int type; //0 : 1 , 2 { 3 } 4 [ 5 ] int pos;// }StructJsonParseMiddle; //事件码和故障码对应的结构体 void JsonParseFloor(QString data,QMap<QString, QString> &map,int floor=1){ int currentFloor = 0; QList<StructJsonParseMiddle> list; for(int i=0;i<data.length();i++){ if(false){} else if(':'==data.at(i)){ list.append({currentFloor,0,i}); } else if(','==data.at(i)){ list.append({currentFloor,1,i}); } else if('{'==data.at(i)){ currentFloor+=1;list.append({currentFloor,2,i}); } else if('['==data.at(i)){ currentFloor+=1;list.append({currentFloor,4,i}); } else if('}'==data.at(i)){ list.append({currentFloor,3,i});currentFloor-=1; } else if(']'==data.at(i)){ list.append({currentFloor,5,i});currentFloor-=1; } } int beginpos = 0; QString key = ""; for(auto line:list){ //myDebugMsg(QString("floor:%1 type:%2 pos:%3").arg(line.floor).arg(line.type).arg(line.pos)); if(line.type==2 && line.floor==floor){beginpos=line.pos+1; } if(line.type==0 && line.floor==floor){ key = data.mid(beginpos,line.pos-beginpos); beginpos=line.pos+1; } if(line.type==1 && line.floor==floor){ map[key]=data.mid(beginpos,line.pos-beginpos); beginpos=line.pos+1; } if(line.type==3 && line.floor==floor){ map[key]=data.mid(beginpos,line.pos-beginpos); beginpos=line.pos+1; } } } void JsonParseArray(QString data, QStringList &res, int floor=1) { int currentFloor = 0; QList<StructJsonParseMiddle> list; for(int i=0;i<data.length();i++){ if(false){} else if(':'==data.at(i)){ list.append({currentFloor,0,i}); } else if(','==data.at(i)){ list.append({currentFloor,1,i}); } else if('{'==data.at(i)){ currentFloor+=1;list.append({currentFloor,2,i}); } else if('['==data.at(i)){ currentFloor+=1;list.append({currentFloor,4,i}); } else if('}'==data.at(i)){ list.append({currentFloor,3,i});currentFloor-=1; } else if(']'==data.at(i)){ list.append({currentFloor,5,i});currentFloor-=1; } } int beginpos = 0; QString key = ""; int flag = 0;//标记是数组还是kv for(auto line:list){ if(line.floor==floor){ if(line.type==2) flag = 2; else if(line.type==4) flag = 4; break; } } if(flag==2){ for(auto line:list){ if(line.type==2 && line.floor==floor){beginpos=line.pos+1; } if(line.type==0 && line.floor==floor){ key = data.mid(beginpos,line.pos-beginpos); beginpos=line.pos+1; } if(line.type==1 && line.floor==floor){ res.append(QString("%1:%2").arg(key).arg(data.mid(beginpos,line.pos-beginpos))); beginpos=line.pos+1; } if(line.type==3 && line.floor==floor){ res.append(QString("%1:%2").arg(key).arg(data.mid(beginpos,line.pos-beginpos))); beginpos=line.pos+1; } } } else if(flag==4){ for(auto line:list){ if(line.type==4 && line.floor==floor){beginpos=line.pos+1; } if(line.type==1 && line.floor==floor){ res.append(data.mid(beginpos,line.pos-beginpos)); beginpos=line.pos+1; } if(line.type==5 && line.floor==floor){ res.append(data.mid(beginpos,line.pos-beginpos)); beginpos=line.pos+1; } } } }
附带另类json解析的函数