假设我有一个嵌套的QJsonObject,它看起来像下面的JSON:
var sample_set_ =
{child_sample_set_objs:[
{sample_containers:[
{"samples":[
{id:1, sample_val:42}
]}
]}
]}
或等效地:
auto sample_set_ =
QJsonObject{
{"child_sample_set_objs",
QJsonArray{
{ QJsonObject{
{"sample_containers",
QJsonArray{
{ QJsonObject{
{"samples,
QJsonArray{
{ QJsonObject{
"id", 1,
"sample_val", 42
} }
}
}
}
} }
}
} }
}
}
}
如何将数据写入叶子节点并变异sample_set_
而不会一直向上复制结构?
以下是我认为我需要做的事情,这是非常冗长的。
给定int child_ss_idx
和int sample_container_idx
:
auto child_sample_set_objs = sample_set_["child_sample_set_objs"].toArray();
auto child_ss = child_sample_set_objs[child_ss_idx].toObject();
auto sample_containers = child_ss["sample_containers"].toArray();
auto sample_container = sample_containers[sample_container_idx].toObject();
auto samples = sample_container["samples"].toArray();
// the write vvvvvvvv
samples.push_back(QJsonObject{ {"sample_set_id", child_ss["id"]} });
sample_container["samples"] = samples;
sample_containers[sample_container_idx] = sample_container;
child_ss["sample_containers"] = sample_containers;
child_sample_set_objs[child_ss_idx] = child_ss;
sample_set_["child_sample_set_objs"] = child_sample_set_objs;