c + json nlohmann 과 poco json 사용

5453 단어 c/c++
C + + nlohmann json 사용 하기
가장 좋 은 c + json 라 이브 러 리 는 nlohmann 입 니 다.
https://blog.csdn.net/wphkadn/article/details/97417700
변 수 를 json 이 라 고 쓰 는 것 은 쉽 지만, json 을 변수 로 바 꾸 는 것 은 좀 복잡 해 야 하지만, nlohmann 에 대해 서 는 조금도 복잡 하지 않다.
 
레 퍼 런 스
http://www.it1352.com/497248.html
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/JSON/Stringifier.h"

using Poco::JSON::ParseHandler;
using Poco::JSON::Stringifier;
using Poco::JSON::Object;
using Poco::JSON::Parser;
using Poco::Dynamic::Var;
using Poco::DynamicStruct;

void objPrint(Object& obj, const std::string& name = "")
{
    for (Object::ConstIterator it = obj.begin(),
        end = obj.end(); it != end; ++it)
    {
        if (!name.empty()) std::cout << name << ':';
        std::cout << it->first << ':';
        if (it->second.type() == typeid(Object::Ptr))
        {
            Object::Ptr p = it->second.extract<:ptr>();
            objPrint(*p, it->first);
        }
        else
             std::cout << it->second.toString() << std::endl;
    }
}

int main(int, char**)
{
    typedef std::map<:string int=""> PersonMap;

    std::string timeStamp = "2015-05-14T17:47:21.999Z";
    Poco::Int32 identifier = 3;
    std::string name = "john smith";
    PersonMap metaData;
    metaData.insert(PersonMap::value_type("william", 45));

    Object obj(true);
    obj.set("ts", timeStamp);
    obj.set("name", name);
    obj.set("identifier", identifier);
    Object::Ptr pMD = new Poco::JSON::Object(true);
    for (PersonMap::iterator it = metaData.begin(),
        end = metaData.end(); it != end; ++it)
    {
        pMD->set("middle_name", it->first);
        pMD->set("age", it->second);
    }
    obj.set("metadata", pMD);

    std::ostringstream os;
    obj.stringify(os, 2);
    std::cout << os.str() << std::endl;

    Parser parser;
    Var result = parser.parse(os.str());

    Object::Ptr pObj = result.extract<:ptr>();
    objPrint(*pObj);

    return 0;
}

 
https://blog.csdn.net/yuhaiyang457288/article/details/47190117?utm_source=blogxgwz2
 
#include
#include 
#include 
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/ParseHandler.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/StreamCopier.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/JSON/Query.h"
#include "Poco/JSON/PrintHandler.h"


using namespace Poco::Dynamic;
using namespace Poco;
using std::string;

// json      。
void JsonGet( )
{
    string jsonString = "{ \"name\" : \"yuhaiyang\" }";
    JSON::Parser parser;
    Dynamic::Var result;
    parser.reset();

    result = parser.parse( jsonString );
    JSON::Object::Ptr pObj = result.extract<:object::ptr>();
    Dynamic::Var ret = pObj->get( "name2" );
    if ( ret.isEmpty() )
    {
        std::cout << "is null" << std::endl;
    }else
    {
        std::cout << ret.toString() << std::endl;
    }
}

//  json      。
void JsonArry( void )
{
    JSON::Object jsnObj;
    JSON::Array jsnArry;
    JSON::Object subObj1;
    JSON::Object subObj2;

    jsnObj.set( "command", "createuser" );

    subObj1.set( "name", "yuhaiyang");
    subObj1.set( "pass", "123" );

    subObj2.set( "name", "cj" );
    subObj2.set( "pass", "456" );

    jsnArry.add( subObj1 );
    jsnArry.add( subObj2 );

    jsnObj.set( "userinfo", jsnArry );

    std::stringstream  jsnString;
    jsnObj.stringify( jsnString, 3 );
    std::cout << jsnString.str() << std::endl;

}


//    
void JsonGetArry( void )
{
/*-------------------    Array JSON(string  )--------------------------------*/
    JSON::Object jsnObj;
    JSON::Array jsnArry;
    JSON::Object subObj1;
    JSON::Object subObj2;

    jsnObj.set( "command", "createuser" );

    subObj1.set( "name", "yuhaiyang");
    subObj1.set( "pass", "123" );

    subObj2.set( "name", "cj" );
    subObj2.set( "pass", "456" );

    jsnArry.add( subObj1 );
    jsnArry.add( subObj2 );

    jsnObj.set( "userinfo", jsnArry );

    std::stringstream  jsnOstream;
    jsnObj.stringify( jsnOstream, 3 );

    string jsnStr = jsnOstream.str();

    std::cout << "   :
" << jsnStr << "


" << std::endl; //-------------------------- ----------------------------- JSON::Parser parse; Dynamic::Var json = parse.parse( jsnStr ); JSON::Object::Ptr pObj = json.extract<:object::ptr>(); JSON::Array::Ptr pArry = pObj->getArray( "userinfo" ); JSON::Array::ConstIterator it = pArry->begin(); // // 。 for ( ; it != pArry->end(); it++ ) { std::cout << it->toString() << std::endl; } } int main() { JsonGet(); JsonArry(); JsonGetArry(); }

 

좋은 웹페이지 즐겨찾기