I have following Json structure.
我遵循了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"
},
],
I have parsed this json response using some parser. Now i want to convert it to C++ struct object.
我已经使用一些解析器解析了这个json响应。现在我想把它转换成C++结构对象。
typedef struct json_object;
struct json_object {
char name;
char city; };
I have to read each href value in each link by looping through JasonParser response object.How can i acheive that in struct.
我必须通过循环遍历JasonParser响应对象来读取每个链接中的每个href值。
Should I use list for the links? How can i do that in struct?
我应该为链接使用列表吗?我如何在结构中做到这一点呢?
Would someone please give example.
谁能举个例子。
更多回答
This is how I would 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 are using it, you could refine it a bit.
根据您使用它的方式,您可以对其进行一些改进。
enum Method {
GET
,POST
};
This could be reasonable, but I think strings are expressive enough until they get in your way.
这可能是合理的,但我认为字符串在它们挡住您的道路之前就足够有表现力了。
Care to use boost? http://www.boost.org/doc/libs/1_49_0/doc/html/boost_propertytree/parsers.html#boost_propertytree.parsers.json_parser
介意使用Boost吗?Http://www.boost.org/doc/libs/1_49_0/doc/html/boost_propertytree/parsers.html#boost_propertytree.parsers.json_parser
This is an old question, but comes up often. I would use https://github.com/beached/daw_json_link and then the following C++ data structures
这是一个老问题,但经常被提出来。我将使用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;
};
In order to map this from the JSON document, the following code is needed
为了从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);
}
};
}
Afterwards, to parse it to the json_object_t
it would be like
然后,要将其解析为json_Object_t,将如下所示
json_object_t json_object = daw::json::from_json<json_object_t>( json_doc );
Json_Object_t json_Object=Daw::json::from_json
(Json_Doc);
Use flex and yacc to implement a parser and code generator to generate code for converting JSON to struct automatically for you.
使用flex和yacc实现解析器和代码生成器,为您自动生成将JSON转换为结构的代码。
Coost provides such a tool gen, define the struct in file xx.proto:
Coost提供了这样一个工具,在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
}]
}
}
then use gen xx.proto to generate the code you need, see the example j2s for details.
然后使用gen xx.proto生成所需的代码,有关详细信息,请参见示例j2S。
更多回答
我是一名优秀的程序员,十分优秀!