Question

I'm using Qjson to parse a json object that is returned from a web service. I'm stuck on handling an array of complex ojects.

At the first level the web service returns a map consisting of "error", "id", and "return". If there are no errors I can get the first level value by using

 nestedMap = m_jsonObject["result"].toMap();
 group = new Group();
 group->Caption = nestedMap["Caption"].toString();
 group->CollectionCount = nestedMap["CollectionCount"].toInt();

I can even get a date item value that is at the second level using

group->ModifiedOn = nestedMap["ModifiedOn"].toMap()["Value"].toDateTime();

I have an object called "Elements" that consists of 29 key-value pairs. The web service is returning an array of these "Elements" and I am unable to find the right way to parse it. In the header file the container for the elements is defined as

QList<GroupElement> Elements;

The line

group->Elements = nestedMap["Elements"].toList();

causes the compiler to throw an error 'error: no match for 'operator=' in '((MyClass*)this)->MyClass::group->Group::Elements = QVariant::toMap() const()'

I would like to learn the correct syntax to put this element into the class.

Was it helpful?

Solution

Update: I wrote another function to convert the QVariantMap object to a

first: The group-> Elements object was changed to a

class ParentClass{
    QList<SharedDataPointer<Address> > Elements;
    other class memmbers...           
};

Second: A method to convert the QMap object to an Address object was created

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;
}

third: In the code, foreach is used to walk through the list and create and store the new objects

// 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))
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top