概述
- 安装jsoncpp
apt-get install libjsoncpp-dev
- 项目中加入头文件
#include <jsoncpp/json/json.h>
- 编译命令后面加上
-ljsoncpp
- 用下面的代码和makefile进行测试,是否安装成功。
//test.cpp
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
using namespace std;
int main() {
Json::Value json_temp;
json_temp["name"] = Json::Value("sharexu");
json_temp["age"] = Json::Value(18);
Json::Value root;
root["key_string"] = Json::Value("value_string");
root["key_number"] = Json::Value(12345);
root["key_boolean"] = Json::Value(false);
root["key_double"] = Json::Value(12.345);
root["key_object"] = json_temp;
root["key_array"].append("array_string");
root["key_array"].append(1234);
Json::StreamWriterBuilder builder;
builder["indentation"] = "";
auto jsonString = Json::writeString(builder, root);
cout << jsonString << endl;
Json::StyledWriter styled_writer;
std::cout << styled_writer.write(root);
string str_test ="{"id":1,"name":"pacozhong"}";
Json::Reader reader;
Json::Value value;
if (!reader.parse(str_test, value))
return 0;
string value_name=value["name"].asString();
cout <<value_name<<endl;
cout <<value["name"];
if(!value["id"].isInt()){
cout<<"id is not int"<<endl;
}else{
int value_id=value["id"].asInt();
cout<<value_id<<endl;
}
return 0;
}
- makefile
test:test.o
g++ -o test test.o -ljsoncpp
test.o:test.cpp
g++ -c test.cpp -ljsoncpp
最后
以上就是有魅力麦片为你收集整理的【jsoncpp】linux c++解析json数据包环境搭建(简洁版)的全部内容,希望文章能够帮你解决【jsoncpp】linux c++解析json数据包环境搭建(简洁版)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复