使用jsoncpp读取json
来源:互联网 发布:地球大炮 知乎 编辑:程序博客网 时间:2024/06/04 18:21
使用jsoncpp读取json
jsoncpp的配置下载jsoncpp的github
添加到工程
方法一:使用Jsoncpp包中的.cpp和.h文件
解压上面下载的jsoncpp-master.zip文件,把jsoncpp-master\include\json文件夹和jsoncpp-master\src\lib_json文件夹里的全部文件拷贝到工程目录下,并且添加到到VS工程中。
在需要使用JsonCpp的文件中包含json头文件即可,如:#include “json/json.h”。另外,需要将json_reader.cpp、json_value.cpp和json_writer.cpp三个文件的Precompiled Header属性设置为Not Using Precompiled Headers,否则编译会出现错误。
方法二:使用Jsoncpp生成的lib文件
解压上面下载的jsoncpp-master.zip文件,在jsoncpp-master\makefiles\vs71目录里找到jsoncpp.sln,用VS编译,默认生成静态链接库。 在工程中引用,只需要包含include/json下的头文件及生成的.lib文件即可。
在需要使用JsonCpp的文件中添加#pragma comment(lib.”json_vc71_libmt.lib”),在工程属性中Linker下Input中Additional Dependencies写入lib文件名字(Release下为json_vc71_libmt.lib,Debug为json_vc71_libmtd.lib)注意:Jsoncpp的lib工程编译选项要和VS工程中的编译选项保持一致。如lib文件工程编译选项为MT(或MTd),VS工程中也要选择MT(或MTd),否则会出现编译错误问题。
json文件
[ { "ColWidth" : [ 33, 63, 53, 60, 63, 66, 47, 63, 57, 65, 65, 64, 66, 85 ], "DlgName" : "窗口1", "FirstRowHeight" : 55, "RowHeight" : 19 }, { "ColWidth" : [ 50, 50, 50, 33, 63, 53 ], "DlgName" : "窗口2", "FirstRowHeight" : 50, "RowHeight" : 30 }]
打开json文件
void JsonOpre::OpenJsonFile(){ CString m_StrFilePath; // 设置过滤器 TCHAR szFilter[] = _T("json文件(*.json)|*.json|所有文件(*.*)|*.*||"); // 构造保存文件对话框 CFileDialog fileDlg(TRUE, _T("读取JSON"), NULL, 0, szFilter); // 显示保存文件对话框 if (IDOK == fileDlg.DoModal()) { // 如果点击了菜单栏上的"另存为"按钮,则选择的保存的文件路径 m_StrFilePath = fileDlg.GetPathName(); std::string str = Auxiliary::CStringToString(m_StrFilePath); if(ParseJsonFromFile(str.c_str())) acedAlert(_T("数据成功!")); else acedAlert(_T("数据保存失败!")); }}
读取json文件
bool JsonOpre::ParseJsonFromFile(const string& filename){ // 解析json用Json::Reader Json::Reader reader; // Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array... Json::Value root; std::ifstream is; CString cfilename = filename.c_str(); is.open (cfilename, std::ios::binary ); if (reader.parse(is, root)) { std::string code; if (root.size() == 0) // 访问节点,Access an object value by name, create a null member if it does not exist. return false; // 遍历数组 DlgAttrVec.clear(); for(int i = 0; i < (int)root.size(); i++) { string Dlgname = root[i]["DlgName"].asString(); int FirstRowHeight = root[i]["FirstRowHeight"].asInt(); int RowHeight = root[i]["RowHeight"].asInt(); int ColWidth_size = root[i]["ColWidth"].size(); vector<int> ColWidthVec; for(int j = 0; j < ColWidth_size; j++) { ColWidthVec.push_back(root[i]["ColWidth"][j].asInt()); } DlgAttrVec.push_back(*(new DlgAttr(Dlgname, FirstRowHeight, RowHeight, ColWidthVec))); } } is.close(); return true; }
存储json文件
bool JsonOpre::DataSaveJsonInFile(const string& filename){ Json::Value root; Json::Value NewDlg; Json::Value arrayColWidth; // 构建对象 root.clear(); for(int i = 0; i < DlgAttrVec.size(); i++){ NewDlg["DlgName"] = DlgAttrVec[i].Dlgname; NewDlg["FirstRowHeight"] = DlgAttrVec[i].FirstRowHeight; NewDlg["RowHeight"] = DlgAttrVec[i].RowHeight; int ColWidth_size = (int)DlgAttrVec[i].ColWidthVec.size(); arrayColWidth.clear(); for(int j = 0; j < ColWidth_size; j++) { arrayColWidth.append(DlgAttrVec[i].ColWidthVec[j]); } NewDlg["ColWidth"] = arrayColWidth; root.append(NewDlg); // 插入数组成员 } //输出(另起一个方法) //转换为字符串(带格式) std::string out = root.toStyledString(); std::ofstream os; CString cfilename = filename.c_str(); os.open (cfilename, std::ios::binary ); os<<out; os.close(); return true;}
- 使用jsoncpp读取json
- cocos2d-x jsonCpp 读取json
- 使用jsoncpp解析json
- 使用jsoncpp解析JSON数据
- C++ 使用Jsoncpp解析Json
- 使用JsonCpp读写Json数据
- C++使用jsoncpp解析json
- 初学c++使用json----jsoncpp
- 使用JsonCPP解析JSON数据(VC++)
- json使用jsoncpp解析,简单方便
- VC下使用JSONCPP(json)
- 使用jsoncpp解析JSON数据(转)
- mac下使用jsoncpp解析json
- QtCreator 中使用jsoncpp解析json
- json解析库jsoncpp的使用
- C++ json类库Jsoncpp的使用
- 使用JsonCPP解析JSON数据 C++
- C++使用Jsoncpp解析Json数据
- eigen与matlab对应函数列表
- Django【进阶篇 】
- 网易云课堂-程序设计入门-C语言-第一周:简单的计算程序
- Elam的caffe笔记之配置篇(四):Centos6.5下配置anaconda3
- HTMl5的sessionStorage和localStorage
- 使用jsoncpp读取json
- Android倒计时CountDownTimer
- dxfgrabber简单读取dxf文件中的多行文本和单行文本信息
- iOS WebView 获取内存图片 图片点击
- 正则表达式
- 文章标题 Mayor's posters
- 字符串的全排列
- map与apply区别
- HashMap Hashtable LinkedHashMap 和TreeMap区别