I have a to write a data into the json key and corresponding to that the value. My key is a structure with two values { int id & char * name}, my value is also a structure with three values { int a, int b, int c }. I intend to put the key in json as { id:name }, and value also separated by delimiter { a:b:c }
How is the conversion done from c++ structure to a json object? and read this created json object as a map again.
I second @Hot Licks about spending 5 minutes to learn the JSON syntax. Maybe even C++.
Assuming a key structure with id=1234 and name='nandini', it will be serialized to:
"{ 'id': 1234, 'name': 'nandini' }"
Ditto for the value structure, that will NOT have the format as you suggested but something like (for a structure with values 1, 2, and 3):
"{ 'a': 1, 'b': 2, 'c': 3 }"
Serialization is easy. You can use whatever string building technique you like, including any variant of sprintf, or stringstream. You can of course use a Json library.
As for the mapping part, Json (or JavaScript for that matter) do not support a keys other than strings, like other languages. If you are using a library that supports std::map - then use it. Otherwise, you can come with something like an array of objects or similar. eg
"[ { key: {...}, val: {...} }, { key: {...}, val: {...} }, ... ]
Then, upon deserialization, put the array into a map.
Deserialization will be trickier, and you should definitely look at a Json library. Take a look of the JSON parsers on JSON.org
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.