Qjson 处理返回的 ojbect 数组



我正在使用 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))
}

最新更新