C++ Json Encode&Decode


项目需要用到C++对Json进行编码和编译,今天整理了下对应的方法做了测试

我用的jsoncpp代码库。
参考文章:C++处理JSON数据和在face++ 调用中的使用

测试代码

#include 
#include 
#include "json/json.h"

using namespace std;

#define IN_FILENAME "data.json"
#define FAST_OUT_FILENAME "fastout.json"
#define STYLE_OUT_FILENAME "styleout.json"

class JsonTest{
public:
    //读取Json文件
    void readJson(){
        ifstream fin(IN_FILENAME);//打开本地文件
        if( !fin )   //错误处理
        {     
            cout << "Error opening " << IN_FILENAME << " for input" << endl;     
            exit(-1);    
        }  
        // const int LINE_LENGTH = 100;
        // char str[LINE_LENGTH];
        // while(fin.getline(str,LINE_LENGTH)){//按行输出
        //  cout<<"Read from file:"<readJson();
    test->writeJson();
}

命令行输入

g++ jsoncpp.cpp jsonTest.cpp -o jsonTest
./jsonTest

进行测试

源码下载

你可能感兴趣的:(C++ Json Encode&Decode)