vs2013 c++ 使用jsoncpp解析json文件

一、自行编译json库
1、先在github上下载源码, json源码下载地址
2、打开makefiles\msvc2010\jsoncpp.sln(利用vs2013打开),会出现三个Project:jsontest, lib_json, test_lib_json
3、选择中间的lib_json项目,右键属性 —》C/C++ —》代码生成—》运行库,选择多线程DLL(/MDd)
4、还是lib_json项目,右键重新生成,就会生成一个lib_json.lib文件。
5、将include\json文件夹下的.h文件拷贝出来,加入到自己的testjson工程包含目录下,将lib_json.lib拷贝到testjson工程的库目录下,并在配置属性—》链接器—》输入中,添加lib_json.lib
6、开始调试自己的testjson工程,可以使用json了。

二、直接下载编译好的库
下载地址为:https://download.csdn.net/download/lly_117/10430803

使用的例子:

#include <iostream>
#include <string>
#include<json/json.h>
using namespace std;

int main()
{
    char* strStudent = "{ \"name\" : \"cuishihao\", \"age\" : 28, \"major\" : \"cs\" }";
    Json::CharReaderBuilder b;
    Json::CharReader* reader(b.newCharReader());
    JSONCPP_STRING errs;
    Json::Value value;
    bool bRet = reader->parse(strStudent, strStudent + std::strlen(strStudent), &value, &errs);
    if (false == bRet)
    {
        cerr << endl << "read value error \n";
        return -1;
    }

    cout << value["name"].asString() << endl;
    cout << value["age"].asInt() << endl;
    cout << value["major"].asString() << endl;

    cout << endl;

    Json::Value json_temp;
    json_temp["name"] = Json::Value("cuihao");
    json_temp["age"] = Json::Value(28);

    Json::Value root;
    root["key_string"] = Json::Value("value_string");
    root["key_number"] = Json::Value(12345);
    root["key_boolean"] = Json::Value(true);
    root["key_double"] = Json::Value(12.345);
    root["key_object"] = json_temp;
    root["key_array"].append("array_string1");
    root["key_array"].append("array_string2");
    root["key_array"].append(12345);
    Json::ValueType type = root.type();

    Json::Value arrValue = root["key_array"];
    for (Json::Value::UInt i = 0; i < arrValue.size(); ++i)
    {
        if (arrValue[i].isString())
            cout << arrValue[i].asString();

        else if (arrValue[i].isInt())
            cout << arrValue[i].asInt();

        cout << endl;
    }

    cout << endl << "----------------------------------- " << endl;


    string strTemp =
        "{ \"name\" :  \"cuihao\" ," \
        " \"age\" : 28 }";

    string strRoot =
        "{ \"key_string\"  :  \"value_string\", " \
        "  \"key_number\"  :  28, "               \
        "  \"key_boolean\" :  false,  "           \
        "  \"key_double\"  :  12.345,  "          \
        "  \"key_object\"  :  json_temp, "        \
        "  \"key_array\"   :  [\"array_string1\",  \"array_string2\", 12345]}";

    Json::StreamWriterBuilder wbuilder;
    wbuilder["indentation"] = "";
    cout << endl << Json::writeString(wbuilder, root) << endl;

    cout << endl << "----------------------------" << endl;


    Json::Value::Members members = root.getMemberNames();

    for (Json::Value::Members::const_iterator iter = members.begin();
        iter != members.end();
        ++iter)
    {
        string strName = *iter;

        if (root[strName].isInt())
            cout << root[strName].asInt();

        else if (root[strName].isString())
            cout << root[strName].asString();

        else if (root[strName].isDouble())
            cout << root[strName].asDouble();

        else if (root[strName].isBool())
            cout << root[strName].asBool();

        else if (root[strName].isArray())
        {
            for (Json::Value::ArrayIndex i = 0; i < root[strName].size(); ++i)
            {
                if (root[strName][i].isInt())
                    cout << root[strName][i].asInt();
                else if (root[strName][i].isString())
                    cout << root[strName][i].asString();
                else
                    cout << "others";

                cout << endl;
            }
        }

        else if (root[strName].isObject())
        {
            Json::Value::Members mbs = root[strName].getMemberNames();
            for (Json::Value::Members::const_iterator iter2 = mbs.begin();
                iter2 != mbs.end();
                ++iter2)
            {
                string strName2 = *iter2;
                if (root[strName][strName2].isInt())
                    cout << root[strName][strName2].asInt();
                else if (root[strName][strName2].isString())
                    cout << root[strName][strName2].asString();
                else
                    cout << "others";

                cout << endl;
            } //for
        } //else if
        else
            cout << "others";

        cout << endl;
    }
    system("pause");
    return 0;
}

其中运行结果如图所示:
这里写图片描述

参考博客:
https://blog.csdn.net/hust_bochu_xuchao/article/details/78516864
https://www.cnblogs.com/cuish/p/3888657.html

猜你喜欢

转载自blog.csdn.net/lly_117/article/details/80412064