Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4463615
  • 博文数量: 356
  • 博客积分: 10458
  • 博客等级: 上将
  • 技术积分: 4734
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-24 14:59
文章分类

全部博文(356)

文章存档

2020年(17)

2019年(9)

2018年(26)

2017年(5)

2016年(11)

2015年(20)

2014年(2)

2013年(17)

2012年(15)

2011年(4)

2010年(7)

2009年(14)

2008年(209)

分类: C/C++

2015-05-12 19:59:55

官网可下载源码,
也可以直接通过git下载源码
git clone

进入目录编译
cd cxxtools/
autoreconf -i
./configure 
make

直接在cxxtools目录下写个测试程序:
test.cpp源码

点击(此处)折叠或打开

  1. #include <fstream>
  2. #include <string>
  3. #include <iostream>
  4. #include "cxxtools/json.h"

  5.     int
  6. main( int argc, char **argv )
  7. {
  8.     std::ifstream input("myconfiguration.json");
  9.     cxxtools::SerializationInfo si;
  10.     input >> cxxtools::Json(si); // parse json into a cxxtools::SerializationInfo

  11.     std::string encoding;
  12.     si.getMember("encoding") >>= encoding;
  13.     std::cout << encoding << std::endl; // prints "UTF-8"

  14.     unsigned length;
  15.     bool use_space;
  16.     si.getMember("indent").getMember("length") >>= length;
  17.     si.getMember("indent").getMember("use_space") >>= use_space;
  18.     // prints "length=3 use_space=true":
  19.     std::cout << "length=" << length << " use_space=" << use_space << std::endl;
  20.     return 0;
  21. }
json的测试文件
myconfiguration.json

{
    "encoding" : "UTF-8",

    "plug-ins" : [
        "python",
        "c++",
        "ruby"
    ],

    "indent" : { "length" : 3, "use_space": true }
}

编译测试文件
g++ test.cpp -I./include -lcxxtools -L./src/.libs
配置环境变量,其中dir表示放置cxxtools的目录 
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:dir/cxxtools/src/.libs
运行
./a.out 
输出如下
UTF-8
length=3 use_space=
不错的库,可以方便地在结构体与json间转换,这个可以看官网上的例子,缺点就是太大了,它不单纯是一个json的解析库。
作者:帅得不敢出门 程序员群:31843264

阅读(2659) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~