使用匹配键打印 JSON - RapidJSON
Print JSON with matching key - RapidJSON
我正在尝试从 JSON 中仅打印与某些键匹配的值:
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
char* kTypeNames[] = { "First", "text", "print", "key" };
int main() {
// 1. Parse a JSON string into DOM.
const char json[] =
" { \"First\" : \"a\", \"text\" : \"b\" ,\"key\" : \"hello\" ,
\"print\" : \"1\",\"print\" : \"2\",\"no_key\" : \"2\"} ";
// ...
Document document;
document.Parse(json);
printf(json);
printf("\nAccess values in document:\n");
assert(document.IsObject());
for (Value::ConstMemberIterator itr = document.MemberBegin();
itr !=document.MemberEnd(); ++itr)
{
//how to print element that matches with kTypeNames array?
}
}
需要的键是 First、text、print 和 key,我想忽略 no_key 值。
所以我只想打印 a, b, hello, 1 不打印 2.
我正在查看文档,试图找出如何做到这一点。
谢谢
您不需要遍历每个成员并查看它是否在 'key' 列表中匹配。而是使用 document.HasMember()
查看密钥是否存在。
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
const char* kTypeNames[] = { "First", "text", "print", "key" };
int main() {
// 1. Parse a JSON string into DOM.
const char json[] =
" { \"First\" : \"a\", \"text\" : \"b\" ,\"key\" : \"hello\" ,\"print\" : \"1\",\"print\" : \"2\",\"no_key\" : \"2\"} ";
// ...
Document document;
document.Parse(json);
printf(json);
printf("\nAccess values in document:\n");
assert(document.IsObject());
//For each type in ktypenames, see if json has member
for (auto Typename : kTypeNames) {
if (document.HasMember(Typename)) {
std::cout << Typename << ":" << document[Typename].GetString() << std::endl;
}
}
}
我正在尝试从 JSON 中仅打印与某些键匹配的值:
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
char* kTypeNames[] = { "First", "text", "print", "key" };
int main() {
// 1. Parse a JSON string into DOM.
const char json[] =
" { \"First\" : \"a\", \"text\" : \"b\" ,\"key\" : \"hello\" ,
\"print\" : \"1\",\"print\" : \"2\",\"no_key\" : \"2\"} ";
// ...
Document document;
document.Parse(json);
printf(json);
printf("\nAccess values in document:\n");
assert(document.IsObject());
for (Value::ConstMemberIterator itr = document.MemberBegin();
itr !=document.MemberEnd(); ++itr)
{
//how to print element that matches with kTypeNames array?
}
}
需要的键是 First、text、print 和 key,我想忽略 no_key 值。 所以我只想打印 a, b, hello, 1 不打印 2.
我正在查看文档,试图找出如何做到这一点。
谢谢
您不需要遍历每个成员并查看它是否在 'key' 列表中匹配。而是使用 document.HasMember()
查看密钥是否存在。
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
const char* kTypeNames[] = { "First", "text", "print", "key" };
int main() {
// 1. Parse a JSON string into DOM.
const char json[] =
" { \"First\" : \"a\", \"text\" : \"b\" ,\"key\" : \"hello\" ,\"print\" : \"1\",\"print\" : \"2\",\"no_key\" : \"2\"} ";
// ...
Document document;
document.Parse(json);
printf(json);
printf("\nAccess values in document:\n");
assert(document.IsObject());
//For each type in ktypenames, see if json has member
for (auto Typename : kTypeNames) {
if (document.HasMember(Typename)) {
std::cout << Typename << ":" << document[Typename].GetString() << std::endl;
}
}
}