Poco::JSON 对象 + 数据 操作

#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;
using namespace Poco::JSON;



const char* g_p ="\
{\
   \"data\" : [\
      {\
         \"iidd\" : \"iiddiiddiiddiiddiiddiidd\",\
         \"msg\" : \"日期:2011-10-14 12:22:54\",\
         \"tag\" : \"tagg\",\
         \"flg\" : \"0\",\
         \"rd1\" : \"网\",\
         \"rd2\" : \"关\",\
         \"rd3\" : \"\",\
         \"rd4\" : \"卡\",\
         \"rd5\" : \"5\"\
      },\
      {\
         \"iidd\" : \"iiddiiddiiddiiddiiddiidd\",\
         \"msg\" : \"日期:2011-10-14 12:22:54\",\
         \"tag\" : \"tagg\",\
         \"flg\" : \"0\",\
         \"rd1\" : \"网\",\
         \"rd2\" : \"关\",\
         \"rd3\" : \"\",\
         \"rd4\" : \"卡\",\
         \"rd5\" : \"5\"\
      }\
   ]\
}";




int main()
{
    JSON::Parser parser;
    Dynamic::Var result;
    parser.reset();
    result = parser.parse(string(g_p));
    JSON::Object::Ptr pObj = result.extract();
    /*
    Dynamic::Var ret = pObj->get("data");
    if ( ret.isEmpty() ) {
    	std::cout << "is null" << std::endl;
    } else {
    	std::cout << ret.toString() << std::endl;
    }
    */
    std::cout << "---------------------------------------------------------------------" << std::endl;
    JSON::Array::Ptr pArry = pObj->getArray("data");
    JSON::Array::ConstIterator it = pArry->begin();

    for ( ; it != pArry->end(); it++ ) {
        //std::cout << it->toString() << std::endl;
        JSON::Object::Ptr pObj = it->extract();
        Dynamic::Var iidd = pObj->get("iidd");
        Dynamic::Var msg = pObj->get("msg");
        Dynamic::Var tag = pObj->get("tag");
        Dynamic::Var flg = pObj->get("flg");
        Dynamic::Var rd1 = pObj->get("rd1");
        Dynamic::Var rd2 = pObj->get("rd2");

        std::cout << iidd.toString() << std::endl;
        std::cout << msg.toString() << std::endl;
        std::cout << tag.toString() << std::endl;
        std::cout << flg.toString() << std::endl;
        std::cout << rd1.toString() << std::endl;
        std::cout << rd2.toString() << std::endl;
    }
    return 0;
}

 

你可能感兴趣的:(Poco,JSON)