使用VS2008编译jsoncpp源码为静态库,在自己工程中使用
1. VS2008
2. MFC工程下使用
3. JSON源码,下载地址:
4. 解压下载的问价:jsoncpp-src-0.5.0.tar.gz jsoncpp源码
5.打开源码的工程E:\vs2008_project\json\jsoncpp-src-0.5.0\makefiles\vs71\jsoncpp.sln
6.生成Debug和Release版本的lib文件,步骤如下:
1>>打开页面如下图1.
2>>(Debug和Release模式方式一样)右键lib_json项目->仅用于项目->仅生产lib_json
3>>再次右键lib_json项目->仅用于项目->仅链接lib_json
4>>生成lib文件在E:\vs2008_project\json\jsoncpp-src-0.5.0\build\vs71下
5>>注意:fatal error C1083: 无法打开编译器生成的文件:“../../build/vs71/release/lib_json\json_writer.asm”: No such file or directory ,一般就是release版本的lib需要在属性设置的时候:配置属性->常规->项目默认值最下面两项改成如图所示.
7. 拷贝 E:\vs2008_project\json\jsoncpp-src-0.5.0\include文件的下的json文件到自己的工程下.
8. 拷贝刚生成的两个lib(如默认名称Debug版本json_vc71_libmtd.lib Release版本json_vc71_libmt.lib)到工程下.目录任意
9.lib文件 + 头文件.h 就可以添加到自己的工程下编译了
1&& 项目->属性->配置属性->C/C++->常规->附加包含目录(jsoncpp源码的头文件.h文件所在位置)
2&& 项目->属性->配置属性->链接器->常规->附加库目录(jsoncpp源码刚生成json_vc71_libmtd.lib和json_vc71_libmt.lib的目录)
3&& 项目->属性->配置属性->链接器->输入->附加依赖项(Debug版本json_vc71_libmtd.lib/Release版本json_vc71_libmt.lib)
10.一般情况下会报错,我的工程师MFC工程,而jsoncpp是win32控制台工程,所以那面容易出错
1## 错误如出现error LNK2005已定义.可修改 项目->属性->配置属性->常规->MFC的使用->在静态库中使用MFC
2## StdAfx.h没有引用可以在 项目->属性->配置属性->C/C++->预编译头->不使用预编译(/Yu)
3## 项目->属性->配置属性->C/C++->代码生成-->运行时库Debug:多线程调试(/MTD) 或者 Release:多线程调试(/MT)
11 .从文件中解析json.
CameraConfig是一个结构体,结构体类型和json转换类型一定要一致,否则崩溃.
- #define JN_bAlarm "bAlarm"
- #define JN_bAutoStart "bAutoStart"
- #define JN_mAlarmCru "mAlarmCru"
- #define JN_mDevIndex "mDevIndex"
- #define JN_mDevIp "mDevIp"
- #define JN_mDevPWD "mDevPWD"
- struct CameraConfig
- {
- bool bAlarm;
- bool bAutoStart;
- int mAlarmCru;
- string mDevIndex;
- string mDevIp;
- string mDevPWD;
- };
读取文件解析json. filename是文件名字
- vector<CameraConfig> parseJsonFromFile(const char* filename)
- {
- vector<CameraConfig> re;
- FILE *file = fopen( filename, "rb" );
- if ( !file )
- {
- return re;
- }
- fseek( file, 0, SEEK_END );
- long size = ftell( file );
- fseek( file, 0, SEEK_SET );
- std::string text;
- char *buffer = new char[size+1];
- buffer[size] = 0;
- if ( !fread( buffer, 1, size, file ) == (unsigned long)size )
- {
- return re;
- }
- text = buffer;
- fclose( file );
- delete[] buffer;
- re = parseJsonFromString(text);
- return re;
- }
- vector<CameraConfig> parseJsonFromString(const std::string str) ///传入字符串解析
- {
- Json::Reader reader;
- Json::Value root;
- vector<CameraConfig> vecCamera;
- if (!reader.parse(str, root)) // reader将Json字符串解析到root,root将包含Json里所有子元素
- {
- return vecCamera;
- }
- int size = 0;
- if (root.isArray())//根是否为数组
- {
- size = root.size();
- // 遍历数组
- for(int i = 0; i < size; ++i)
- {
- CameraConfig camera;
- camera.bAlarm = root[i][JN_bAlarm].asBool();
- camera.bAutoStart = root[i][JN_bAutoStart].asBool();
- camera.mAlarmCru = root[i][JN_mAlarmCru].asInt();
- camera.mDevIndex = root[i][JN_mDevIndex].asString();
- camera.mDevIp = root[i][JN_mDevIp].asString();
- camera.mDevPWD = root[i][JN_mDevPWD].asString();
- vecCamera.push_back(camera);
- }
- }
- else//根目录不是数组时
- {
- CameraCongig camera;
- camera.bAlarm = root[JN_bAlarm].asBool();
- camera.bAutoStart = root[JN_bAutoStart].asBool();
- camera.mAlarmCru = root[JN_mAlarmCru].asInt();
- camera.mDevIndex = root[JN_mDevIndex].asString();
- camera.mDevIp = root[JN_mDevIp].asString();
- camera.mDevPWD = root[JN_mDevPWD].asString();
- vecCamera.push_back(camera);
- }
- return vecCamera;
- }
- void writeJsonFile(vector<CameraConfig> &vecCamera, const string &path)
- {
- Json::StyledWriter sw; //按照标准格式写入
- Json::Value root; //根为数组
- for (int i = 0; i < vecCamera.size(); i++)//循环加入数组内容到json节点中
- {
- root[i][JN_bAlarm] = Json::Value(vecCamera.at(i).bAlarm);
- root[i][JN_bAutoStart] = Json::Value(vecCamera.at(i).bAutoStart);
- root[i][JN_mAlarmCru] = Json::Value(vecCamera.at(i).mAlarmCru);
- root[i][JN_mDevIndex] = Json::Value(vecCamera.at(i).mDevIndex);
- root[i][JN_mDevIp] = Json::Value(vecCamera.at(i).mDevIp);
- root[i][JN_mDevPWD] = Json::Value(vecCamera.at(i).mDevPWD);
- }
- ofstream os;
- os.open(path.c_str());
- os << sw.write(root); //写入文件
- os.close();
- }
- [ camera.cfg文件如下 根目录为数组,数组名字为空
- {
- "bAlarm" : false,
- "bAutoStart" : false,
- "mAlarmCru" : 0,
- "mDevIndex" : "小明",
- "mDevIp" : "172.16.3.245",
- "mDevPWD" : "12345",
- },
- {
- "bAlarm" : false,
- "bAutoStart" : false,
- "mAlarmCru" : 0,
- "mDevIndex" : "小王",
- "mDevIp" : "172.16.3.248",
- "mDevPWD" : "12345", }
- ]
- <span style="font-size:24px;">//将信息保存为JSON格式 ,内嵌套格式</span>
- <pre name="code" class="cpp">
- void writeFileJson()
- {
- //根节点
- Json::Value root;
- //根节点属性
- root["name"] = Json::Value("Mike Jiang");
- root["age"] = Json::Value(23);
- root["sex_is_male"] = Json::Value(true);
- //子节点
- Json::Value partner;
- //子节点属性
- partner["partner_name"] = Json::Value("Galatea");
- partner["partner_age"] = Json::Value(21);
- partner["partner_sex_is_male"] = Json::Value(false);
- //子节点挂到根节点上
- root["partner"] = Json::Value(partner);
- //数组形式
- root["achievement"].append("ach1");
- root["achievement"].append("ach2");
- root["achievement"].append("ach3");
- //直接输出
- cout << "FastWriter:" << endl;
- Json::FastWriter fw;
- cout << fw.write(root) << endl << endl;
- //缩进输出
- cout << "StyledWriter:" << endl;
- Json::StyledWriter sw;
- cout << sw.write(root) << endl << endl;
- //输出到文件
- ofstream os;
- os.open("demo.json");
- os << sw.write(root);
- os.close();
- }
- 1. 从字符串解析json
- const char* str = "{\"uploadid\": \"UP000000\",\"code\": 100,\"msg\": \"\",\"files\": \"\"}";
- Json::Reader reader;
- Json::Value root;
- if (reader.parse(str, root)) // reader将Json字符串解析到root,root将包含Json里所有子元素
- {
- std::string upload_id = root["uploadid"].asString(); // 访问节点,upload_id = "UP000000"
- int code = root["code"].asInt(); // 访问节点,code = 100
- }
- 2. 从文件解析json //读取文件test.json
- int ReadJsonFromFile(const char* filename)
- {
- Json::Reader reader;// 解析json用Json::Reader
- Json::Value root; // Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array
- std::ifstream is;
- is.open (filename, std::ios::binary );
- if (reader.parse(is, root, FALSE))
- {
- std::string code;
- if (!root["files"].isNull()) // 访问节点,Access an object value by name, create a null member if it does not exist.
- { code = root["uploadid"].asString(); }
- code = root.get("uploadid", "null").asString();// 访问节点,Return the member named key if it exist, defaultValue otherwise.
- int file_size = root["files"].size(); // 得到"files"的数组个数 嵌套类型
- for(int i = 0; i < file_size; ++i) // 遍历数组
- {
- Json::Value val_image = root["files"][i]["images"];
- int image_size = val_image.size();
- for(int j = 0; j < image_size; ++j) //嵌套二层
- {
- std::string type = val_image[j]["type"].asString();
- std::string url = val_image[j]["url"].asString();
- printf("type : %s, url : %s \n", type.c_str(), url.c_str());
- }
- }
- }
- is.close();
- return 0;
- }
- 3. 向文件中插入json
- void WriteJsonData(const char* filename)
- {
- Json::Reader reader;
- Json::Value root; // Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array
- std::ifstream is;
- is.open (filename, std::ios::binary );
- if (reader.parse(is, root))
- {
- Json::Value arrayObj; // 构建对象
- Json::Value new_item, new_item1;
- new_item["date"] = "2011-11-11";
- new_item1["time"] = "11:11:11";
- arrayObj.append(new_item); // 插入数组成员
- arrayObj.append(new_item1); // 插入数组成员
- int file_size = root["files"].size();
- for(int i = 0; i < file_size; ++i)
- root["files"][i]["exifs"] = arrayObj; // 插入原json中
- std::string out = root.toStyledString();
- // 输出无格式json字符串
- Json::FastWriter writer;
- std::string strWrite = writer.write(root);
- std::ofstream ofs;
- ofs.open("test_write.json");
- ofs << strWrite;
- ofs.close();
- }
- }
- {
- "uploadid": "UP000000",
- "code": "0",
- "msg": "",
- "files":
- [
- {
- "code": "0",
- "msg": "",
- "filename": "1D_16-35_1.jpg",
- "filesize": "196690",
- "width": "1024",
- "height": "682",
- "images":
- [
- {
- "url": "fmn061/20111118",
- "type": "large",
- "width": "720",
- "height": "479"
- },
- {
- "url": "fmn061/20111118",
- "type": "main",
- "width": "200",
- "height": "133"
- }
- ]
- }
- ]
- }
- {
- "code" : "0",
- "files" : [
- {
- "code" : "0",
- "exifs" : [
- {
- "date" : "2011-11-11"
- },
- {
- "time" : "11:11:11"
- }
- ],
- "filename" : "1D_16-35_1.jpg",
- "filesize" : "196690",
- "height" : "682",
- "images" : [
- {
- "height" : "479",
- "type" : "large",
- "url" : "fmn061/20111118",
- "width" : "720"
- },
- {
- "height" : "133",
- "type" : "main",
- "url" : "fmn061/20111118",
- "width" : "200"
- }
- ],
- "msg" : "",
- "width" : "1024"
- }
- ],
- "msg" : "",
- "uploadid" : "UP000000"
- }