PDA

View Full Version : Build json, how to?



martinn
12th March 2010, 13:40
Hi!

If found out from this example http://qtwiki.org/Parsing_JSON_with_QT_using_standard_QT_library how easy it was to parse json with Qt. Nice! But what if I need the other way around? I need to build json from QObject instance, how to do that?

Thanks!

martinn
13th March 2010, 10:00
Anyone now how to do this?

wysota
13th March 2010, 10:22
Iterate over properties of QObject and compose the json string based on the property name and property value converted to string (for scalar values) or lists or maps (for lists and maps).

squidge
13th March 2010, 10:24
EDIT: Wysota got there before me.

martinn
14th March 2010, 15:26
Thanks for the answer! I'm quite a newbie, do you have some example code that I can take a look at, it would be very very helpful! Thanks again!

wysota
14th March 2010, 16:19
Use QMetaObject associated with your object to query for names of static properties or use QObject::dynamicPropertyNames() for dynamic properties. Then query the object for value of each of the properties and compose a string using the json format.