ubuntu jsoncpp 1.9.5 编译使用

下载

jsoncpp 1.9.5 好像不支持scons编译了,使用cmake编译

原始仓库: https://github.com/open-source-parsers/jsoncpp
gitee仓库:https://gitee.com/mirrors/jsoncpp/tree/master

选择版本下载
在这里插入图片描述

编译

搞到linux上解压执行cmake CMakeLists.txt,发现了编译攻略:

在这里插入图片描述
按照上面的操作:
创建目录:
在这里插入图片描述

注:这边cp的文件是重新解压缩的,上面已经执行过cmake CMakeLists.txt目录的目录已经被我删掉了。
或者把cp这一步换成git clone https://github.com/open-source-parsers/jsoncpp.git也可以

生成makefile
在这里插入图片描述
make
在这里插入图片描述

拷贝头文件和库文件

将链接库拷贝到/usr/lib
在这里插入图片描述
将头文件拷贝到/usr/include
在这里插入图片描述

测试代码

网上随便copy一个代码1.cpp

#include <json/json.h>
#include <string>
#include <iostream>
using namespace std;

int main()
{
	//std::string strValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}";

	// 构建json数组
	Json::Value array;
	Json::Value root;
	Json::Value person;

	Json::FastWriter writer;

	person["name"] = "allen";
	person["age"] = 10;
	person["sex"] = "male";
	root.append(person);

	person["name"] = "keiv";
	person["age"] = 20;
	person["sex"] = "female";
	root.append(person);

	person["name"] = "lihua";
	person["age"] = 10;
	person["sex"] = "female";
	root.append(person);

	// 添加数组格式
	// array["array"].append(root);

	// 子节点挂到根节点上
	array["array"] = Json::Value(root);

	string data = writer.write(array);

	cout<<data<<endl;
	// cout<<array.toStyledString()<<endl;

	// 解析Json字符串
	string strValue = array.toStyledString(); // json对象转变为json字符串
	cout << strValue << endl;

	Json::Reader reader;
	Json::Value value;

	if (reader.parse(strValue, value)) // json字符串转为json对象
	{
		for (unsigned int i = 0; i < value["array"].size(); i++)
		{
			string name = value["array"][i]["name"].asString();
			int age = value["array"][i]["age"].asInt();
			string sex = value["array"][i]["sex"].asString();

			cout << name << " " << age << " " << sex << endl;
		}
	}

	return 0;
}

编译运行:g++ 1.cpp -ljsoncpp
在这里插入图片描述
大功告成!

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值