我正在尝试以 sax 方式使用 rapidjson 编写 json 结构。这就是我所做的:
StringBuffer sb;
PrettyWriter<StringBuffer> writer(sb);
writer.StartObject();
writer.Key("user");
writer.StartArray();
OperatorAcPtrList::iterator xIt;
for (xIt = userList.begin(); xIt != userList.end(); xIt++)
{
writer.Key("userId");
writer.Uint((*xIt)->id);
writer.Key("productIdList");
writer.StartArray();
ProductIdList::iterator xPrdIdIt;
for (xPrdIdIt = ((*xIt)->productList).begin();
xPrdIdIt != ((*xIt)->productList).end(); xPrdIdIt++)
{
writer.Uint(*xPrdIdIt);
}
writer.EndArray();
}
writer.EndArray();
writer.EndObject();
但结果不是我所期望的,它是:
{
"userList": [
"userId",
20,
"productIdList",
[
1,
2
],
"userId",
21,
"productIdList",
[
1,
2
]
]
}
看起来第一个 StartArray EndArray 中的所有内容都变成了一个数组元素。我想获得的是:
{
"userList": [
{
"userId" : 20,
"productIdList" : [1, 2],
},
{
"userId" : 21,
"productIdList" : [1, 2]
}
]
}
我做错了什么还是根本不支持我想要的?