jsoncpp在c++中的使用

来源:互联网 发布:变换域自适应滤波算法 编辑:程序博客网 时间:2024/05/21 04:16

一,     简介

1.json是一种轻量级的数据交换格式,而jsoncpp是一个跨平台的开源库。

2.jsoncpp主要包含三种类型的类:Value,Reader,Writer。

3.jsoncpp中所有的对象和类都在namespace json中,只要包含头文件json.h即可。

二,     jsoncpp的安装(ubuntu)

1.   安装scons

apt-get install scons

2.   下载源码包

http://sourceforge.net/projects/jsoncpp/files/ 

3.   依次执行以下命令

tar -zxf jsoncpp-src-0.5.0.tar.gz

cd jsoncpp-src-0.5.0 

scons platform=linux-gcc

mv libs/linux-gcc-4.8/libjson_linux-gcc-4.8_libmt.so  /usr/local/lib

mkdir /usr/local/include/json

mv include/json/* /usr/local/include/json/

4.   编译

g++ 1_json.cpp/usr/local/lib/libjson_linux-gcc-4.8_libmt.so

三,     使用详解

1.   从字符串解析json

#include <stdio.h>

#include <json/json.h>

int main()

{

        char* sTest ="{\"name\":\"lixuemei\",\"age\":23}";

        Json::Reader reader;

        Json::Value val;

        bool bRet;

        bRet =reader.parse(sTest,val);

        if(bRet)

        {

               printf("name:%s,age:%d\n",val["name"].asString().c_str(),val["age"].asInt());

        }

        else

        {

               printf("parse error!\n");

        }

        return 0;

}

 

运行结果:

name:lixuemei,age:23

 

2.   结构体转换为json格式字符串

 

// struct data to json string

#include <stdio.h>

#include <string.h>

#include <json/json.h>

typedef struct{

        char name[20];

        int age;

}person;

typedef struct{

        char name[20];

        int age;

        int friendnum;

        person friends[2];

}student;

int main()

{

        student sTestStruct;

        Json::Value jsonValue;

        Json::Value item;

 

       memset(&sTestStruct,0,sizeof(student));

       memcpy(sTestStruct.name,"lixuemei",8);

        sTestStruct.age = 23;

        sTestStruct.friendnum= 2;

       memcpy(sTestStruct.friends[0].name,"lina",4);

       sTestStruct.friends[0].age = 23;

       memcpy(sTestStruct.friends[1].name,"zhengyin",8);

       sTestStruct.friends[1].age = 21;

 

        // struct to jsonvalue

       jsonValue["name"] = sTestStruct.name;

        jsonValue["age"]= sTestStruct.age;

       jsonValue["friendnum"] = sTestStruct.friendnum;

        for(int i = 0; i <2;i++)

        {

               item["name"] = sTestStruct.friends[i].name;

               item["age"] = sTestStruct.friends[i].age;

                jsonValue["friends"].append(item);

        }

        std::string jsonstring= jsonValue.toStyledString();

       printf("jsonstring:%s\n",jsonstring.c_str());

 

        return 0;

}

运行结果:

jsonstring:{

   "age" : 23,

   "friendnum" : 2,

   "friends" : [

      {

         "age" : 23,

         "name" :"lina"

      },

      {

         "age" : 21,

         "name" :"zhengyin"

      }

   ],

   "name" :"lixuemei"

}

3.json数据写入文件

 int save_to_file(Json::Value& json_value,string file_name)
{
    ofstream log_file_;
    log_file_.open (file_name.c_str(),ofstream::out | ofstream::trunc);
    Json::StyledWriter writer;
    log_file_<< writer.write(json_value);
    log_file_.close();
    
    return 0;
}

0 0
原创粉丝点击