Json to C ++ struct

I have the following Json structure.

{ "name": "abc", "city": "holland", "links": [ { "href": "/city/holland/1", "method": "GET", "rel": "edit", "type": "application/holland.citydata+json" }, links": [ { "href": "/city/holland/2", "method": "GET", "rel": "self", "type": "application/holland.citydata+json" }, ], 

I parsed this json answer using some parser. Now I want to convert it to a C ++ structure object.

 typedef struct json_object; struct json_object { char name; char city; }; 

I need to read every href value in every link, going through the JasonParser response object. How can I get this in a struct.

Should I use a list of links? How can I do this in a struct?

I would try, please, an example.

+4
source share
2 answers

This is how I do it.

 struct Link { std::string href; std::string method; std::string rel; std::string type; }; struct JSONObject { std::string name; std::string city; std::vector<Link> links; }; 

Depending on how you use it, you can refine it a bit.

 enum Method { GET ,POST }; 

This may be reasonable, but I think the lines are expressive enough, as long as they don't bother you.

+2
source

Source: https://habr.com/ru/post/1403785/


All Articles