json到c ++ struct

use*_*083 5 c c++ json

我有以下Json结构.

{
"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"
    },

], 
Run Code Online (Sandbox Code Playgroud)

我使用一些解析器解析了这个json响应.现在我想将其转换为C++ struct对象.

typedef struct json_object;
struct json_object {

char name;
char city; };
Run Code Online (Sandbox Code Playgroud)

我必须通过循环遍历JasonParser响应对象来读取每个链接中的每个href值.如何在struct中实现它.

我应该使用列表链接吗?我怎么能在结构中这样做?

有人请举例.

Tom*_*err 2

我就是这样做的。

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;
};
Run Code Online (Sandbox Code Playgroud)

根据您使用它的方式,您可以对其进行一些改进。

enum Method {
  GET
  ,POST
};
Run Code Online (Sandbox Code Playgroud)

这可能是合理的,但我认为字符串具有足够的表现力,直到它们妨碍您为止。