解析json
来源:互联网 发布:美工全职 编辑:程序博客网 时间:2024/06/08 12:59
需求:根据原来标注的json拿来重新改成训练LSTM需要的格式。先上代码吧,公司网截图上传不了。回去再完善
json的格式如下:
其中的label对应的是图片上的字符
LSTM需要的数据格式如下图,也是最终输出的out文本里的
这里需要说下的是,对应的字符需要转成类别数字,还有这是车架号的字符,听说没有o q,所以去掉这两个字符,0-9,a-10,b-11,字符要转成数字类别,在代码里我第一次用了map这种数据结构,对应关系,map<char,int>....... 代码根据json里面label后面就是接的字符来解析的。在这里注意一下,我用了stringstream这种流,把ing型转化为string型的,调试了许久,老是不对劲,明明每次clear了还是每次出现好多输出,原来,clear只是重置了其标志位,真正的清空需要ss.str(""),这有点奇葩了。
下面是程序跑起来打印的map对应关系:
#include <iostream>#include<string>#include<vector>#include<fstream>#include<sstream>#include<map>using namespace std;int main(int argc, char *argv[]){ string img_name; string ImgName_Label; string str; string str_content; stringstream ss; ifstream list_json_path("/media/d/Yang/project_8-9/data/json/list_json_path.txt"); ofstream out("/media/d/Yang/project_8-9/data/out.txt"); map<char,int> Mymap; int flg=10; for(char i=97;i<=122;i++) { if((i=='o')||(i=='q')) continue; Mymap[i]=flg++; } map<char,int>::iterator it; it=Mymap.begin(); while (it!=Mymap.end()) { cout<<it->first<<";"<<it->second<<endl; ++it; } while(list_json_path>>str) { cout<<str<<endl; std::string::size_type pos_begin=str.find_last_of("/"); std::string::size_type pos_end=str.find_first_of("."); img_name=str.substr(0,pos_end-0+4); ImgName_Label=img_name; ifstream json_file(str); json_file>>str_content; std::string::size_type pos_tmp=str_content.find("label"); while (pos_tmp!=string::npos) { ImgName_Label+=" "; char ch=(char)str_content[pos_tmp+8]; int tmp_map=0; if(ch>=97&&ch<=122) { // cout<<ch<<" "; // ImgName_Label+=str_content[pos_tmp+8]; tmp_map=Mymap[ch]; ss<<tmp_map; ImgName_Label+=ss.str(); ss.str(""); ss.clear(); } else { ImgName_Label+=str_content[pos_tmp+8]; } pos_tmp=str_content.find("label",pos_tmp+8); } out<<ImgName_Label<<endl; } return 0;}
其实这里面还有一个bug,那个是需要图片的地址的,而out文本里面的地址是json的地址,后来训练的时候报错才发现的,把地址栏的json换成mark就好了,因为的图片是放在和它同目录下的mark里面的。
阅读全文
0 0
- 【JSON解析】JSON解析
- 解析JSON
- Json解析
- JSON解析
- JSON解析
- json解析
- Json解析
- Json解析
- JSON解析
- Json解析
- Json解析
- JSON解析
- JSON解析
- json 解析
- Json解析
- JSON解析
- json解析
- json 解析
- 访问修饰符与方法重写
- 收好,带"\t"的九九乘法表
- 两不同程序之间通讯
- Hbase学习笔记:初识HBase
- 如何用Java实现邮件发送?---- 网站通过邮箱激活底层代码
- 解析json
- xpath插件
- AsynTask 原理
- HDU1260 动态规划 简单DP
- (适合初学,理清思路)ssm配置按步骤详解
- HDU 1250-Hat's Fibonacci(大数)
- final关键字
- Set 集合以及Set集合的一些常用方法
- Python_13