JSON转换为C++结构体

6

I have 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"
    },

], 

我使用某个解析器对此JSON响应进行了解析。现在我想将其转换为C++结构对象。
typedef struct json_object;
struct json_object {

char name;
char city; };

我需要通过循环遍历JasonParser响应对象来读取每个链接中的href值。如何在struct中实现?

我应该使用列表来存储链接吗?如何在struct中实现?

能否请有经验的人提供一个例子。

4个回答

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

根据您的使用情况,您可以进行一些改进。

enum Method {
  GET
  ,POST
};

这个想法也许是合理的,但我认为字符串在不妨碍你的情况下已经足够表达了。



0

这是一个老问题,但经常出现。我会使用https://github.com/beached/daw_json_link,然后使用以下C++数据结构

  struct links_element_t {
    std::string href;
    std::string method;
    std::string rel;
    std::string type;
  };

  struct json_object_t {
    std::string name;
    std::string city;
    std::vector<links_element_t> links;
  };

为了从JSON文档中进行映射,需要使用以下代码。
#include <daw/daw_json_link.h>
namespace daw::json {
template <>
struct json_data_contract<links_element_t> {
  static constexpr char const mem_href[] = "href";
  static constexpr char const mem_method[] = "method";
  static constexpr char const mem_rel[] = "rel";
  static constexpr char const mem_type[] = "type";
  using type = json_member_list<json_string<mem_href>, json_string<mem_method>,
                                json_string<mem_rel>, json_string<mem_type>>;

  static inline auto to_json_data(links_element_t const& value) {
    return std::forward_as_tuple(value.href, value.method, value.rel,
                                 value.type);
  }
};

template <>
struct json_data_contract<root_object_t> {
  static constexpr char const mem_name[] = "name";
  static constexpr char const mem_city[] = "city";
  static constexpr char const mem_links[] = "links";
  using type = json_member_list<
      json_string<mem_name>, json_string<mem_city>,
      json_array<mem_links, json_class<no_name, links_element_t>,
                 std::vector<links_element_t>>>;

  static inline auto to_json_data(root_object_t const& value) {
    return std::forward_as_tuple(value.name, value.city, value.links);
  }
};
}

接下来,将其解析为json_object_t的方法如下:

json_object_t json_object = daw::json::from_json<json_object_t>( json_doc );


0
使用flex和yacc来实现一个解析器和代码生成器,为你自动生成将JSON转换为结构体的代码。 Coost提供了这样一个工具gen,在文件xx.proto中定义结构体。
package xx
    
// supported base types:
//   bool, int, int32, uint32, int64, uint64, double, string
object X {
    string api
    data {  // anonymous object, field name can be put ahead
        bool b
        int i
        double d
        [int] ai   // array of int
        ao [{      // array of anonymous object
            int v
            string s
        }]
    }
}

然后使用gen xx.proto来生成你所需的代码,详细信息请参见示例j2s

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接