我该如何在C++中解析嵌套的 JSON?我正在寻找一个能够解析嵌套 JSON 的 JSON 解析器,特别是下面示例 JSON 中的字段:
optional<variant<bool, Secondary>> secondary;
它是optional
和variant
的类型组合。
虽然只有完整的示例才能更清楚地展现问题,但以下是最小起始点示例:
[
{},
{
"secondary": false
},
{
"secondary": {
"chance": 10,
"boosts": {
"spd": -1
}
}
},
{
"secondary": {
"chance": 30,
"volatileStatus": "flinch"
}
},
{
"secondary": {
"chance": 30
}
},
{
"secondary": {
"chance": 10,
"self": {
"boosts": {
"atk": 1,
"def": 1,
"spa": 1,
"spd": 1,
"spe": 1
}
}
}
},
{
"secondary": {
"chance": 10,
"status": "brn"
}
},
{
"secondary": {
"chance": 50,
"self": {
"boosts": {
"def": 2
}
}
}
},
{
"secondary": {
"chance": 100,
"self": {}
}
},
{
"secondary": {
"chance": 50,
"boosts": {
"accuracy": -1
}
}
}
]
以下是我已经有的内容:
struct Boosts {
optional<int> atk;
optional<int> def;
};
struct Self {
optional<Boosts> boosts;
};
struct Secondary {
optional<int> chance;
optional<string> volatileStatus;
optional<Boosts> boosts;
optional<Self> self;
optional<string> status;
};
struct move_t {
int num;
variant<bool, int> accuracy;
int basePower;
string category;
optional<string> desc = std::nullopt;
string shortDesc;
string id;
string name;
optional<variant<bool, Secondary>> secondary;
};
battle
)在json对象j
中,则我希望使用j.battle
而不是j["battle"]
来引用它。 - lmln