Poco::JSONオブジェクト+データ操作

3014 ワード

#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<:object::ptr>();
    /*
    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<:object::ptr>();
        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;
}