Json解析 --JsonCpp

Posted 尚书左仆射

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Json解析 --JsonCpp相关的知识,希望对你有一定的参考价值。

1、下载scons

http://sourceforge.net/projects/scons/files/scons/2.1.0/scons-2.1.0.tar.gz/download

2、解压scons-2.1.0.tar.gz

tar -zvxf scons-2.1.0.tar.gz

3、进入到解压目录scons-2.1.0下面,执行命令:

sudo python setup.py install

4、下载JsonCpp 最新版本

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

5、解压jsconcpp

tar -zvxf jsoncpp-src-0.5.0.tar.gz

6、进入到jsoncpp解压目录(jsoncpp-src-0.5.0)下,执行命令:

scons platform=linux-gcc 

7、将/jsoncpp-src-0.5.0/include/目录下的json文件夹拷贝到需要使用的项目下(其中包含的都是一些所需的头文件)

8、将jsoncpp-src-0.5.0/libs/linux-gcc-4.8.5/目录下的libjson_linux-gcc-4.9.1_libmt.a 重命名为libjson.a,并拷贝到/usr/local/lib/下

包含json头文件后若出现如下一系列报错:

/usr/include/sys/cdefs.h:42:20: error: missing binary operator before token "("
 # if __GNUC_PREREQ (4, 6) && !defined _LIBC
                    ^
/usr/include/sys/cdefs.h:55:44: error: missing binary operator before token "("
 # if !defined __cplusplus && __GNUC_PREREQ (3, 3)
                                            ^
/usr/include/sys/cdefs.h:60:44: error: missing binary operator before token "("
 #  if defined __cplusplus && __GNUC_PREREQ (2,8)
                                            ^
/usr/include/sys/cdefs.h:144:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (4,3)
                   ^
/usr/include/sys/cdefs.h:157:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (2,97)
                   ^
/usr/include/sys/cdefs.h:219:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (2,96)
                   ^
/usr/include/sys/cdefs.h:228:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (2,96)

Makefile中-I参数设置为 -I./include ,不要直接设置为-I./include/json,不然容易导致头文件冲突。增加 -ljson 编译参数。

测试json:


    "title": "title value",
    "version":"3.2",
    "json.url": "https://www.json.com",
    "keywords": "show json",
    "function": ["JSON DATA",
    "JSON BUILD FORMAT",
    "JSON ARRAY",
    "hightlight"]

测试代码:

void test_json()

    //create json data and save it
    string test="\\"id\\":1,\\"name\\":\\"lisa\\",\\"address\\":\\"hangzhou\\"";
    Json::Reader reader;
    Json::FastWriter writer;
    Json::Value value;
    if(reader.parse(test,value)) 
        if(!value["id"].isNull()) 
            cout<<value["id"].asInt()<<endl;
            cout<<value["name"].asString()<<endl;
            cout<<value["address"].asString()<<endl;
        
        //save to file
        string str = writer.write(value);
        cout << str << endl;

        ofstream ofs;
        ofs.open("./settings/json/code_create.json");
        ofs << str;
        ofs.close();
    

    //read json data
    Json::Value json_object;
    ifstream ifs;
    ifs.open("./settings/json/info.json", ios::binary);
    if (!reader.parse(ifs, json_object))
        cout << "open error" << endl;
        ifs.close();
    else
        cout << "title:" << json_object["title"].asString() << endl;
        cout << "version:" <<json_object["version"].asString() << endl;

        int num = json_object["function"].size();
        for (int i = 0; i < num; i++)
            if(json_object["function"][i].isArray())
            
                Json::Value array_obj = json_object["function"][i];
                cout<<"----------array"<<endl;
                for(int idx = 0; idx < array_obj.size(); ++idx)
                
                    cout << array_obj[i].asString() << " ";
                
                cout<<"----------"<<endl;

                continue;
            

            cout << json_object["function"][i].asString() <<endl;
        
        ifs.close();
        cout << endl;
    

运行结果:

运行后产生的json文件:

Json元素类型校验:

bool isNull();

bool isChar();

bool isUChar();

bool isShort();

bool isUShort();

bool isLong();

bool isULong();

bool isInt();

bool isUInt();

bool isInt64();

bool isUInt64();

bool isBoolean();

bool isFloat();

bool isDouble();

bool isString();

bool isObject();

bool isArray();

 

json数据类型访问 

const char* asCString() const;

JSONCPP_STRING asString() const;

Int asInt() const;

UInt asUInt() const;

Int64 asInt64() const;

UInt64 asUInt64() const;

LargestInt asLargestInt() const;

LargestUInt asLargestUInt() const;

float asFloat() const;

double asDouble() const;

bool asBool() const;

以上是关于Json解析 --JsonCpp的主要内容,如果未能解决你的问题,请参考以下文章

JsonCpp解析和读写Json字符串

JsonCpp解析和读写Json字符串

Json---使用Jsoncpp解析与写入

JsonCpp——json文件的解析

详解将jsoncpp编译so库,进行封装和解析JSON数据

C++ jsoncpp 输出名称和值