Qjson 处理返回的 ojbect 数组
Qjson handling an returned array of ojbects
我正在使用 Qjson 来解析从 Web 服务返回的 json 对象。 我坚持处理一系列复杂的 ojects。
在第一级,Web 服务返回一个由"error"、"id"和"return"组成的映射。 如果没有错误,我可以通过以下方式获取第一级值
nestedMap = m_jsonObject["result"].toMap();
group = new Group();
group->Caption = nestedMap["Caption"].toString();
group->CollectionCount = nestedMap["CollectionCount"].toInt();
我什至可以使用
group->ModifiedOn = nestedMap["ModifiedOn"].toMap()["Value"].toDateTime();
我有一个名为"元素"的对象,它由 29 个键值对组成。 Web 服务返回这些"元素"的数组,我找不到正确的方法来解析它。 在头文件中,元素的容器定义为
QList<GroupElement> Elements;
该行
group->Elements = nestedMap["Elements"].toList();
导致编译器抛出错误"错误:与"((MyClass*)this)->MyClass::group->Group::Elements = QVariant::toMap() const()"中的"operator="不匹配
我想学习正确的语法来将此元素放入类中。
更新:我写了另一个函数将 QVariantMap 对象转换为
第一:组>元素对象已更改为
class ParentClass{
QList<SharedDataPointer<Address> > Elements;
other class memmbers...
};
第二:创建了将 QMap 对象转换为地址对象的方法
QSharedDataPointer<Address>
API_1_6::mapToAddress(QVariantMap o)
{
QSharedDataPointer<Address> address (new Address());
address-> FirstName = o["FirstName"].toString();
address->LastName = o["LastName"].toString();
address->CompanyName = o["CompanyName"].toString();
address->Street = o["Street"].toString();
address->Street2 = o["Street2"].toString();
address->City = o["City"].toString();
address->Zip = o["Zip"].toString();
address-> State = o["State"].toString();
address->Country = o["Country"].toString();
address->Phone = o["Phone"].toString();
address->Phone2 = o["Phone2"].toString();
address-> Fax = o["Fax"].toString();
address-> Url = o["Url"].toString();
address->Email = o["Email"].toString();
address->Other = o["Other"].toString();
return address;
}
第三:在代码中,foreach 用于遍历列表并创建和存储新对象
// get the list of the elements
elementsList = nestedMap["Elements"].toList();
// Add the element, converted to the new type, to the Elements object of the'parent' class
foreach(QVariant qElement, elementsList){
group-> Elements.append(mapToAddress(qElement))
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '