C++:使用poco创建 JSON

目录

一、编译、安装poco

二、使用poco创建JSON

三、编译及运行示例代码


一、编译、安装poco

参考:https://blog.csdn.net/fengbohello/article/details/118335212

二、使用poco创建JSON

#include <iostream>
#include <Poco/JSON/Parser.h>

int main(int argc, char **argv)
{
    Poco::JSON::Object json;
    json.set(std::string("key"), std::string("value"));

    Poco::JSON::Object subjson;
    subjson.set(std::string("subkey"), std::string("subvalue"));

    json.set(std::string("sub"), subjson);

    std::ostringstream oss;
    json.stringify(oss);
    std::cout << oss.str() << std::endl;

    return 0;
}

Object::stringify() 函数将 Poco::JSON::Object 转为 string 形式,函数声明和定义如下:

poco/JSON/include/Poco/JSON/Object.h:

namespace Poco {
namespace JSON {

class JSON_API Object
{
public:
    void stringify(std::ostream& out, unsigned int indent = 0, int step = -1) const;
        /// Prints the object to out stream.
        ///
        /// When indent is 0, the object will be printed on a single
        /// line without indentation.
}

三、编译及运行示例代码

编译:

g++ -o json-maker json-maker.cpp --std=c++17  -l PocoJSON -l PocoFoundation -I /usr/local/include/

运行:

$ LD_LIBRARY_PATH=/usr/local/lib ./json-maker 
{"key":"value","sub":{"subkey":"subvalue"}}

注意:如果 poco 安装在了 /usr/ 目录中,那么就不需要 LD_LIBRARY_PATH 环境变量了,如果安装在了其它地方,那就需要使用 LD_LIBRARY_PATH 指定 poco 动态链接库所在的位置。


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值