任何人都可以指导我 Poco C++ JSON 如何工作吗?
之前我使用过 JsonReader 和 JsonToken。 Poco C++库好像没有对应的对象。
例如,我如何使用 json 解析器创建一个由标签名称处的 JSON 值组成的对象名称?
编辑:从 1.5.2 开始,通过设置 DefaultHandler 来简化事情,嗯...默认(并将其重命名为正确的名称 - ParseHandler)。因此,如果您需要的只是解析,则无需显式提供处理程序不再:
// 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);
请参阅此答案了解更多详细信息。
#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
从 Alex 的片段(索引到 Json 数组)中,我创建了一个工作示例,并在 Json 数组上添加了一个循环。
g++ -std=c++20 -o json-array-poco json-array-poco.cpp -lPocoRedis -lPocoUtil -lPocoNet -lPocoNetSSL -lPocoCrypto -lPocoXML -lPocoJSON -lPocoFoundation
sudo apt install libpoco-dev
存储到json-array-poco.cpp中
#include <iostream>
#include <Poco/JSON/JSON.h>
#include <Poco/JSON/Parser.h>
#include <stack>
using Poco::JSON::Parser;
using std::cout;
using std::stack;
using std::string;
void example_index_into_array (){
Parser parser;
string json_string = R"( {"data":[{"p":11,"t":1111},{"p":22,"t":2222}],"foo":"bar"} )";
auto json_pocojson = parser.parse(json_string).extract<Poco::JSON::Object::Ptr>(); // Specify type as Json
auto foo_string = json_pocojson->get("foo").extract<string>(); // Specify type as string
auto data_pjarray = json_pocojson->get("data").extract<Poco::JSON::Array::Ptr>(); // Specify type as Json array
auto item_0 = data_pjarray->getObject(0);
auto item_1 = data_pjarray->getObject(1);
assert (foo_string == "bar");
assert (item_0->getValue<int64_t>("t") == 1111);
assert (item_1->getValue<int64_t>("t") == 2222);
assert (item_0->getValue<int64_t>("p") == 11);
assert (item_1->getValue<int64_t>("p") == 22);
}
void example_loop_over_array (){
Parser parser;
string json_string = R"( {"data":[{"p":11,"t":1111},{"p":22,"t":2222}],"foo":"bar"} )";
auto json_pocojson = parser.parse(json_string).extract<Poco::JSON::Object::Ptr>(); // Specify type as Json
auto data_pjarray = json_pocojson->get("data").extract<Poco::JSON::Array::Ptr>(); // Specify type as Json array
stack<int> t;
stack<int> p;
t.push(2222);
t.push(1111);
p.push(22);
p.push(11);
// The Poco JSON array is not a C11 range, so a range-base for loop errors on compile with
// this range-based 'for' statement requires a suitable "begin" function and none was foundC/C++(2291)
// The pre-range method is the only option
for (auto item_pjaiterator = data_pjarray->begin(); item_pjaiterator != data_pjarray->end(); ++item_pjaiterator) {
auto item_pocojson = item_pjaiterator->extract<Poco::JSON::Object::Ptr>();
assert (item_pocojson->get("t").extract<int64_t>() == t.top()); t.pop();
assert (item_pocojson->get("p").extract<int64_t>() == p.top()); p.pop();
}
}
int main() {
cout << "This is json-array-poco\n";
example_index_into_array();
example_loop_over_array();
return 0;
}