zoukankan      html  css  js  c++  java
  • 如何对接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 
    ";
            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
    
  • 相关阅读:
    锚点
    DOM
    background
    Vue前端路由
    JavaScript常用方法
    算法——dfs介绍
    时间复杂度和空间复杂度
    CSS定位(position)
    CSS三栏布局
    前端笔试高频知识点汇总
  • 原文地址:https://www.cnblogs.com/bugutian/p/13214295.html
Copyright © 2011-2022 走看看