Utilisation correcte de Poco C++ JSON pour l'analyse des données

2 réponses

EDIT: à partir de 1.5.2, les choses ont été simplifiées en faisant DefaultHandler, Eh bien ... par défaut (et de la renommer en son nom propre - ParseHandler. Ainsi, si tout ce dont vous avez besoin est l'analyse, plus besoin de fournir explicitement le gestionnaire:

// objects
std::string json = "{ \"test\" : { \"property\" : \"value\" } }";
Parser parser;
Var result = parser.parse(json);
Object::Ptr object = result.extract<Object::Ptr>();
Var test = object->get("test");
object = test.extract<Object::Ptr>();
test = object->get("property");
std::string value = test.convert<std::string>();

// array of objects
std::string json = "[ {\"test\" : 0}, { \"test1\" : [1, 2, 3], \"test2\" : 4 } ]";
Parser parser;
Var result = parser.parse(json);
Array::Ptr arr = result.extract<Array::Ptr>();
Object::Ptr object = arr->getObject(0);//
assert (object->getValue<int>("test") == 0);
object = arr->getObject(1);
arr = object->getArray("test1");
result = arr->get(0);
assert (result == 1);

Voir cette réponse pour plus de détails.

29
répondu Alex 2017-05-23 11:47:04
#include <iostream>
#include <string>
#include <Poco/JSON/JSON.h>
#include <Poco/JSON/Parser.h>
#include <Poco/Dynamic/Var.h>

using namespace std;
using namespace Poco::JSON;

string GetValue(Object::Ptr aoJsonObject, const char *aszKey) {
    Poco::Dynamic::Var loVariable;
    string lsReturn;
    string lsKey(aszKey);

    // Get the member Variable
    //
    loVariable = aoJsonObject->get(lsKey);

    // Get the Value from the Variable
    //
    lsReturn = loVariable.convert<std::string>();

    return lsReturn;
}

int main(int argc, char *argv[]) {
    string lsJson;
    Parser loParser;

    lsJson = "{\"TransactionCode\":\"000000\",\"FileRecordSequenceNumber\":\"111111\",\"TcrSequenceNumber\":\"222222\",\"TransactionRouteIndicator\":\"ABCDE\",\"MerchantEstablishmentNumber\":\"00000000000\",\"MerchantName\":\"BBBBBBBBB\",\"MerchantCity\":\"CCCCCCCC\"}";

    cout << lsJson << endl;

    // Parse the JSON and get the Results
    //
    Poco::Dynamic::Var loParsedJson = loParser.parse(lsJson);
    Poco::Dynamic::Var loParsedJsonResult = loParser.result();

    // Get the JSON Object
    //
    Object::Ptr loJsonObject = loParsedJsonResult.extract<Object::Ptr>();

    // Get the values for the member variables
    //
    //
    cout << "TransactionCode             " << GetValue(loJsonObject, "TransactionCode")               << endl;
    cout << "FileRecordSequenceNumber    " << GetValue(loJsonObject, "FileRecordSequenceNumber")      << endl;
    cout << "TcrSequenceNumber           " << GetValue(loJsonObject, "TcrSequenceNumber")             << endl;
    cout << "TransactionRouteIndicator   " << GetValue(loJsonObject, "TransactionRouteIndicator")     << endl;
    cout << "MerchantEstablishmentNumber " << GetValue(loJsonObject, "MerchantEstablishmentNumber")   << endl;
    cout << "MerchantName                " << GetValue(loJsonObject, "MerchantName")                  << endl;
    cout << "MerchantCity                " << GetValue(loJsonObject, "MerchantCity")                  << endl;

    return 0;

}

Results:
{"TransactionCode":"000000","FileRecordSequenceNumber":"111111","TcrSequenceNumber":"222222","TransactionRouteIndicator":"ABCDE","MerchantEstablishmentNumber":"00000000000","MerchantName":"BBBBBBBBB","MerchantCity":"CCCCCCCC"}
TransactionCode             000000
FileRecordSequenceNumber    111111
TcrSequenceNumber           222222
TransactionRouteIndicator   ABCDE
MerchantEstablishmentNumber 00000000000
MerchantName                BBBBBBBBB
MerchantCity                CCCCCCCC
15
répondu David Murry 2013-09-12 23:50:18