C++如何对接JOSN(如何使用jsoncpp)?

如何对接jsoncpp?
参考:https://www.cnblogs.com/cuish/p/3888657.html

下载源码

https://sourceforge.net/projects/jsoncpp/files/latest/download
https://sourceforge.net/projects/scons/files/latest/download

解压

$ ls
jsoncpp-src-0.5.0 
scons-2.3.4

编译脚本

cd jsoncpp-src-0.5.0
python ../scons-2.3.4/script/scons platform=linux-gcc
gccversion=`gcc --version | grep "^gcc"|awk '{print $3}'`
libPath=`echo "linux-gcc-"${gccversion}`
cp -rf include/* /usr/local/include
cp -f  libs/${libPath}/*.a  /usr/local/lib/libjson.a
cd -

写个demo测试

(这个是抄别人的,网上有,省的自己写了,而且人家的demo写的还挺好的,一眼就让人知道怎么用了)

#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <json/json.h> 

using namespace std;

int main()
{ 

    string strStudent = "{ \"name\" : \"cuishihao\", \"age\" : 28, \"major\" : \"cs\" }";

    Json::Reader reader;
    Json::Value value;
    bool bRet = reader.parse(strStudent, value);
    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::StyledWriter writer;
    cout << endl << writer.write(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;
    }

    return 0;
}

编译

g++ demo.cpp -I /usr/local/include/ -L/usr/local/lib/ -ljson -o demo
#或者
g++ demo.cpp -I /usr/local/include/ /usr/local/lib/libjson.a -o demo

运行

$ ./demo
cuishihao
28
cs

array_string1
array_string2
12345

----------------------------------- 

{
   "key_array" : [ "array_string1", "array_string2", 12345 ],
   "key_boolean" : true,
   "key_double" : 12.3450,
   "key_number" : 12345,
   "key_object" : {
      "age" : 28,
      "name" : "cuihao"
   },
   "key_string" : "value_string"
}


----------------------------
array_string1
array_string2
12345

1
12.345
12345
28
cuihao

value_string

如果集成过程中报错:

/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)
                   ^
/usr/include/sys/cdefs.h:235:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (2,5)
                   ^
/usr/include/sys/cdefs.h:244:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (3,1)
                   ^
/usr/include/sys/cdefs.h:253:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (3,2)
                   ^
/usr/include/sys/cdefs.h:265:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (2,8)
                   ^
/usr/include/sys/cdefs.h:275:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (2,97)
                   ^
/usr/include/sys/cdefs.h:284:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (3,3)
                   ^
/usr/include/sys/cdefs.h:292:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (3,4)
                   ^
/usr/include/sys/cdefs.h:306:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (3,2)
                   ^
/usr/include/sys/cdefs.h:314:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (4,3)
                   ^
/usr/include/sys/cdefs.h:329:44: error: missing binary operator before token "("
 #if (!defined __cplusplus || __GNUC_PREREQ (4,3) \
                                            ^
/usr/include/sys/cdefs.h:348:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (4,3)
                   ^
/usr/include/sys/cdefs.h:357:20: error: missing binary operator before token "("
 #if !__GNUC_PREREQ (2,8)
                    ^
/usr/include/sys/cdefs.h:362:20: error: missing binary operator before token "("
 #if !__GNUC_PREREQ (2,92)
                    ^
/usr/include/sys/cdefs.h:369:19: error: missing binary operator before token "("
 #if __GNUC_PREREQ (3,1) && !defined __GNUG__
                   ^
In file included from /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/c++config.h:2097:0,
                 from /usr/include/c++/4.8.2/iostream:38,
                 from ../../source/Main.cpp:16:
/usr/include/c++/4.8.2/x86_64-redhat-linux/bits/os_defines.h:44:19: error: missing binary operator before token "("
 #if __GLIBC_PREREQ(2,15) && defined(_GNU_SOURCE)
                   ^
In file included from /usr/include/c++/4.8.2/cwchar:44:0,
                 from /usr/include/c++/4.8.2/bits/postypes.h:40,
                 from /usr/include/c++/4.8.2/iosfwd:40,
                 from /usr/include/c++/4.8.2/ios:38,
                 from /usr/include/c++/4.8.2/ostream:38,
                 from /usr/include/c++/4.8.2/iostream:39,
                 from ../../source/Main.cpp:16:
/usr/include/wchar.h:74:43: error: missing binary operator before token "("
 # if defined __cplusplus && __GNUC_PREREQ (4, 4)
                                           ^
In file included from /usr/include/sched.h:42:0,
                 from /usr/include/pthread.h:23,
                 from /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/gthr-default.h:35,
                 from /usr/include/c++/4.8.2/x86_64-redhat-linux/bits/gthr.h:148,
                 from /usr/include/c++/4.8.2/ext/atomicity.h:35,
                 from /usr/include/c++/4.8.2/bits/ios_base.h:39,
                 from /usr/include/c++/4.8.2/ios:42,
                 from /usr/include/c++/4.8.2/ostream:38,
                 from /usr/include/c++/4.8.2/iostream:39,
                 from ../../source/Main.cpp:16:
/usr/include/bits/sched.h:132:20: error: missing binary operator before token "("
 # if __GNUC_PREREQ (2, 91)
                    ^
/usr/include/bits/sched.h:170:20: error: missing binary operator before token "("
 # if __GNUC_PREREQ (2, 91)
                    ^
In file included from ../../source/Main.cpp:22:0:
/usr/include/string.h:35:42: error: missing binary operator before token "("
 #if defined __cplusplus && __GNUC_PREREQ (4, 4)
                                          ^

那就说明在编译指定include目录时,把json包进去了。比如"-I include/json",因为在头文件引用过程中写的是#include"json/json.h",其实没必要包含json文件夹的。
解决方法编译时改为"-I include"即可。当然,不要觉得多多益善,如果两个目录都包含,文件冲突了,仍然会报上面的错误。

原文链接: https://www.cnblogs.com/bugutian/p/13214295.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++如何对接JOSN(如何使用jsoncpp)?

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/360701

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 下午1:27
下一篇 2023年3月2日 下午1:27

相关推荐