[C++]Jsoncpp的使用
2016-11-22 21:06
232 查看
1.引言
json是现在移动互联网如此发达的年代主流的通信协议。本文介绍的是json的c++版本的实现:jsoncpp库。
2.项目地址
本文的demo地址为:https://github.com/lixiang0/demos/tree/master/jsoncpp
3.项目目录的说明
3.1.打开项目
jsoncpp\makefiles\vs71目录为vs2013工程项目。双击jsoncpp.sln即可打开本文的项目。3.2.lib_json配置
设置jsontest为“启动项目”;右键项目“lib_json”,点击“属性”->配置属性->常规->输出目录,在输出目录中配置lib库文件的输出路径。
点击“确定”关闭对话框。
3.3.jsontest配置
右键项目“jsontest”,点击->属性->配置属性->链接器->附加依赖项,在附加依赖项中配置上一步操作项目“lib_json”生成的lib库文件。以上就可以运行本项目了。
下面是本文的main函数代码以及部分解释。
#include <iostream> #include <string> #include <json\json.h> using namespace std; int main(int argc, char* argv[]) { char* str1 = "{\"cmd\":100,\"result\":false}"; char* str2 = "{\"cmd\":101,\"content\":{\"username\":\"user\",\"passwd\":\"123\"}}"; char* str3 = "{\"cmd\":102,\"userlist\":[{\"name\":\"name1\",\"age\":23},{\"name\":\"name2\",\"age\":26}]}"; Json::Reader reader; Json::Value value; //解析str1 cout << "start paser str1..." << endl; bool result = reader.parse(str1,value); if (!result){ cout << "json paser str1 error!" << endl; } cout <<"cmd:" <<value["cmd"].asInt() << endl; cout << "result:" << value["result"].asBool() << endl; cout << "end paser str1..." << endl; //解析str2 cout << "start paser str2..." << endl; result = reader.parse(str2, value); if (!result){ cout << "json paser str2 error!" << endl; } cout << "cmd:" << value["cmd"].asInt() << endl; cout << "username:" << value["content"]["username"].asString() << endl; cout << "passwd:" << value["content"]["passwd"].asString() << endl; cout << "end paser str2..." << endl; //解析str3 cout << "start paser str3..." << endl; result = reader.parse(str3, value); if (!result){ cout << "json paser str3 error!" << endl; } cout << "cmd:" << value["cmd"].asInt() << endl; Json::Value arrValue = value["userlist"]; for (Json::Value::ArrayIndex i = 0; i < arrValue.size(); i++){ cout << "name:" << arrValue[i]["name"].asString(); cout << " age:" << arrValue[i]["age"].asInt() << endl; } cout << "end paser str3..." << endl; //构造str1 Json::StyledWriter writer; Json::Value root; root["cmd"] = "100"; root["result"] = false; cout << writer.write(root) << endl; //构造str2 Json::Value root2; root2["cmd"] = "101"; Json::Value temp; temp["username"] = "user"; temp["passwd"] = "123"; root2["conent"] = temp; cout << writer.write(root2) << endl; //构造str3 Json::Value root3; root3["cmd"] = "102"; Json::Value temp3; temp3["username"] = "name1"; temp3["age"] = "23"; Json::Value temp2; temp2["username"] = "name2"; temp2["age"] = "26"; root3["userlist"].append(temp3); root3["userlist"].append(temp2); cout << writer.write(root3) << endl; cin.get(); return 0; }
相关文章推荐
- 【C++】JSON数据解析——jsoncpp的使用
- 【C++】JSON数据解析——jsoncpp的使用(下)
- C++简单使用Jsoncpp来读取写入json文件
- C++使用Jsoncpp解析Json数据
- c++使用jsoncpp(vs2015)
- c++使用jsonCpp和boost库解析json总结
- C++实现json转换,使用JsonCpp第三方库,详细说明
- C++ JSONCPP使用
- c++使用Protobuf Message转Json字符串(Json库使用Json cpp)
- c++ 第三方json解析库 jsoncpp的使用
- C++中使用Curl和JsonCpp调用有道翻译API实现在线翻译
- C++ 解析Json VC6 中编译、使用jsoncpp
- C++ 使用JsonCpp
- C++使用jsoncpp解析json
- C++ JsonCpp 使用(含源码下载)
- 【C++】JSON数据解析——jsoncpp的使用(上)
- c++ 第三方json解析库 jsoncpp的使用
- C++ json类库Jsoncpp的使用
- vs2013 c++ 编译配置jsoncpp和使用jsoncpp解析json文件
- 初学c++使用json----jsoncpp