gpt4 book ai didi

c - JSON - 对象的嵌套

转载 作者:行者123 更新时间:2023-11-30 17:39:52 25 4
gpt4 key购买 nike

如何在 C 编程中将 [ 和 ] 附加到 json 对象......

我有类似的东西

      json_builder_begin_object(builder);
json_builder_set_member_name( builder, "response" );
json_builder_add_string_value( builder, "success" );

json_builder_set_member_name( builder, "data" );
json_builder_begin_object( builder );
json_builder_set_member_name( builder, "type" );
json_builder_add_string_value( builder, "vehicle " );
json_builder_set_member_name( builder, "value" );
json_builder_add_string_value( builder, ">10lakhs " );
json_builder_set_member_name(builder,"color");
json_builder_add_string_value(builder,"Black");

我需要这样的回复

[{"response":"成功"},"data"[{"type":"vehicle"},{"value":">100万美元"},{"color":"black"}]]

-提前致谢..:) :)

最佳答案

您必须使用 set_member_name 和 begin_object。前者设置子对象的名称,后者初始化子对象,以便其余数据进入其中。

例如,来 self 的Countly app :

  json_builder_begin_object (builder);

if(crash->hardware){
json_builder_set_member_name (builder, "hardware");
json_builder_begin_object(builder);
if(crash->hardware->os){
json_builder_set_member_name (builder, "_os");
json_builder_add_string_value (builder, crash->hardware->os);
}

if(crash->hardware->os_version) {
json_builder_set_member_name (builder, "_os_version");
json_builder_add_string_value (builder, crash->hardware->os_version);
}
if(crash->hardware->manufacture) {
json_builder_set_member_name (builder, "_manufacture");
json_builder_add_string_value (builder, crash->hardware->manufacture);
}
if(crash->hardware->device) {
json_builder_set_member_name (builder, "_device");
json_builder_add_string_value (builder, crash->hardware->device);
}
if(crash->hardware->resolution) {
json_builder_set_member_name (builder, "_resolution");
json_builder_add_string_value (builder, crash->hardware->resolution);
}
if(crash->hardware->app_version) {
json_builder_set_member_name (builder, "_app_version");
json_builder_add_string_value (builder, crash->hardware->app_version);
}
if(crash->hardware->cpu) {
json_builder_set_member_name (builder, "_cpu");
json_builder_add_string_value (builder, crash->hardware->cpu);
}
if(crash->hardware->opengl) {
json_builder_set_member_name (builder, "_opengl");
json_builder_add_string_value (builder, crash->hardware->opengl);
}
json_builder_end_object (builder);

}

关于c - JSON - 对象的嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21670041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com