• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C++ parse_json函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C++中parse_json函数的典型用法代码示例。如果您正苦于以下问题:C++ parse_json函数的具体用法?C++ parse_json怎么用?C++ parse_json使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了parse_json函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。

示例1: parse_json

bool parse_json(ConnectionInfo& info, const json& j) {
	try {
		GET_NODE_ENUM(j, "type", ConnectionType, info.Type);
		GET_NODE_ENUM(j, "channel", RC_CHANNEL, info.Channel);
		GET_NODE_STRING(j, "dev_sn", info.DevSN, RC_MAX_SN_LEN);

		bool ret = true;
		switch(info.Type) {
		case CT_SERIAL:
			ret = parse_json(info.Serial, j["info"]);
			break;
		case CT_TCPC:
			ret = parse_json(info.TCPClient, j["info"]);
			break;
		case CT_UDP:
			ret = parse_json(info.UDP, j["info"]);
			break;
		case CT_TCPS:
			ret = parse_json(info.TCPServer, j["info"]);
			break;
		case CT_PLUGIN:
			ret = parse_json(info.Plugin, j["info"]);
			break;
		case CT_TEST:
		default:
			break;
		}
		return ret;
	}
	catch (...) {
		return false;
	}
}
开发者ID:kooiot,项目名称:rdc,代码行数:33,代码来源:DataJson.cpp


示例2: parse_json

json_t *json_loads(const char *string, json_error_t *error)
{
    lex_t lex;
    json_t *result;

    string_data_t stream_data = {
        .data = string,
        .pos = 0
    };

    if(lex_init(&lex, string_get, string_eof, (void *)&stream_data))
        return NULL;

    result = parse_json(&lex, error);
    if(!result)
        goto out;

    lex_scan(&lex, error);
    if(lex.token != TOKEN_EOF) {
        error_set(error, &lex, "end of file expected");
        json_decref(result);
        result = NULL;
    }

out:
    lex_close(&lex);
    return result;
}

json_t *json_loadf(FILE *input, json_error_t *error)
{
    lex_t lex;
    json_t *result;

    if(lex_init(&lex, (get_func)fgetc, (eof_func)feof, input))
        return NULL;

    result = parse_json(&lex, error);
    if(!result)
        goto out;

    lex_scan(&lex, error);
    if(lex.token != TOKEN_EOF) {
        error_set(error, &lex, "end of file expected");
        json_decref(result);
        result = NULL;
    }

out:
    lex_close(&lex);
    return result;
}
开发者ID:dcollien,项目名称:Humperdink-C,代码行数:52,代码来源:load.c


示例3: test_request

static void
test_request(const char *json)
{
    jsmntok_t tokens[32];
    int parsed = parse_json(
        tokens, LGTD_ARRAY_SIZE(tokens), json, strlen(json)
    );

    bool ok;
    struct lgtd_jsonrpc_request req = TEST_REQUEST_INITIALIZER;
    struct lgtd_client client = {
        .io = NULL, .current_request = &req, .json = json
    };
    ok = lgtd_jsonrpc_check_and_extract_request(&req, tokens, parsed, json);
    if (!ok) {
        errx(1, "can't parse request");
    }

    lgtd_jsonrpc_check_and_call_set_light_from_hsbk(&client);

    if (set_light_called) {
        errx(1, "lgtd_proto_power_off was called");
    }

    reset_client_write_buf();
}
开发者ID:aoighost,项目名称:lightsd,代码行数:26,代码来源:test_jsonrpc_check_and_call_set_light_from_hsbk_invalid_params.c


示例4: parse_json

json_t *json_loads(const char *string, json_error_t *error)
{
    lex_t lex;
    json_t *result;

    string_data_t stream_data = {
        /*.data = */string,
        /*.pos = */0
    };

    if(lex_init(&lex, string_get, string_eof, (void *)&stream_data))
        return NULL;

    result = parse_json(&lex, error);
    if(!result)
        goto out;

    lex_scan(&lex, error);
    if(lex.token != TOKEN_EOF) {
        error_set(error, &lex, "end of file expected");
        json_decref(result);
        result = NULL;
    }

out:
    lex_close(&lex);
    return result;
}
开发者ID:pavlog,项目名称:cgminer,代码行数:28,代码来源:load.c


示例5: main

int main(int argc, char* argv[])
{
  char* buffer;
  size_t length, body_pos;
  struct artist_album_state state = {0};
  int i;

  setlocale(LC_ALL, "");

  install_sighandler();

  httpreq(&buffer, &length);

  body_pos = http_body_offset(buffer, length);

  LOG(LOG_OK, "New relases:");
  parse_json(buffer + body_pos, length - body_pos, (void*)&state, artist_album_cb);

  for (i = 0; i < state.result_index; i++) {
    printf("%d. %s\n", i, state.result[i]);
  }

  spotify_main_loop(argv[1], argv[2], spotify_callback);

  free(buffer);

  for (i = 0; i < state.result_index; i++) {
   free(state.result[i]);
  }

  return 0;
}
开发者ID:padenot,项目名称:Serendipite,代码行数:32,代码来源:main.c


示例6: benchmarkFile

void benchmarkFile(char *filename)
{
    clock_t begin, end;
    double time_spent;
    
    json_token *tokens = calloc(sizeof(json_token), 3000000);
    
    json_parser parser;
    json_parser_init(&parser);
    
    
    parser.tokens = tokens;
    parser.maxTokens = 3000000;
    
    
    parser_from_file(&parser, filename);
    
    
    
    
    begin = clock();
    
    /* parse the json document */
    parse_json(&parser, RFC4627);
    
    
    end = clock();
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
    
    printResults(&parser, filename, time_spent);
    
    file_parser_free(&parser);
    
    free(tokens);
}
开发者ID:aderussell,项目名称:arsn_json_parser,代码行数:35,代码来源:main.c


示例7: jsonp_error_init

json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
{
    lex_t lex;
    const char *source;
    json_t *result;

    if(input == stdin)
        source = "<stdin>";
    else
        source = "<stream>";

    jsonp_error_init(error, source);

    if (input == NULL) {
        error_set(error, NULL, "wrong arguments");
        return NULL;
    }

    if(lex_init(&lex, (get_func)fgetc, input))
        return NULL;

    result = parse_json(&lex, flags, error);

    lex_close(&lex);
    return result;
}
开发者ID:cryptobiote,项目名称:jansson,代码行数:26,代码来源:load.c


示例8: do_notify

static void
do_notify(struct ovs_cmdl_context *ctx)
{
    struct jsonrpc_msg *msg;
    struct jsonrpc *rpc;
    struct json *params;
    struct stream *stream;
    const char *method;
    char *string;
    int error;

    method = ctx->argv[2];
    params = parse_json(ctx->argv[3]);
    msg = jsonrpc_create_notify(method, params);
    string = jsonrpc_msg_is_valid(msg);
    if (string) {
        ovs_fatal(0, "not a JSON RPC-valid notification: %s", string);
    }

    error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
                              DSCP_DEFAULT), &stream);
    if (error) {
        ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]);
    }
    rpc = jsonrpc_open(stream);

    error = jsonrpc_send_block(rpc, msg);
    if (error) {
        ovs_fatal(error, "could not send notification");
    }
    jsonrpc_close(rpc);
}
开发者ID:AlexanderChou,项目名称:ovs,代码行数:32,代码来源:test-jsonrpc.c


示例9: main

int main(int argc, char **argv)
{
    if(argc < 3) {
        fprintf(stderr, "%s out.db file1.json file2.json ... fileN.json\n", 
                argv[0]);
        return 0;
    }

    struct cdb_make cdbm;
    int fd;
    fd = open("tmp.db", O_RDWR | O_CREAT, 0644);
    cdb_make_start(&cdbm, fd);

    argv++;
    argc--;
    char *dbname = *argv;
    argv++;
    argc--;

    int i;
    for(i = 0; i < argc; i++) {
        parse_json(&cdbm, argv[0]); 
        argv++;
    }

    cdb_make_finish(&cdbm);
    rename("tmp.db", dbname);
    close(fd);
    return 0;
}
开发者ID:narner,项目名称:Sporth,代码行数:30,代码来源:cdbjson.c


示例10: on_post_messages_request

static int8_t on_post_messages_request(int8_t cid, GSwifi::GSREQUESTSTATE state) {
    while (! gs.bufferEmpty()) {
        char letter = gs.bufferGet();
        parse_json( letter );
    }

    if (state == GSwifi::GSREQUESTSTATE_RECEIVED) {
        // should be xmitting or idle (xmit finished)
        if (IrCtrl.state == IR_WRITING) {
            HTTPLOG_PRINTLN("!E7");
            // invalid json
            gs.writeHead(cid, 400);
            gs.writeEnd();
        }
        else {
            gs.writeHead(cid, 200);
            gs.writeEnd();
        }
        ring_put( &commands, COMMAND_CLOSE );
        ring_put( &commands, cid );

#ifdef USE_INTERNET
        TIMER_START( suspend_polling_timer, SUSPEND_GET_MESSAGES_INTERVAL );
#endif
    }

    return 0;
}
开发者ID:deton,项目名称:irkit-device,代码行数:28,代码来源:IRKitHTTPHandler.cpp


示例11: main

int
main(void)
{
    jsmntok_t tokens[32];
    memset(tokens, 0, sizeof(tokens));
    const char json[] = "[[\"*\"],[1,2,3,4]]";
    int parsed = parse_json(
        tokens, LGTD_ARRAY_SIZE(tokens), json, sizeof(json)
    );

    struct lgtd_jsonrpc_target_args {
        const jsmntok_t *target;
        int             target_ntokens;
        const jsmntok_t *label;
    } params = { NULL, 0, NULL };
    static const struct lgtd_jsonrpc_node schema[] = {
        LGTD_JSONRPC_NODE(
            "target",
            offsetof(struct lgtd_jsonrpc_target_args, target),
            offsetof(struct lgtd_jsonrpc_target_args, target_ntokens),
            lgtd_jsonrpc_type_string_number_or_array,
            false
        ),
        LGTD_JSONRPC_NODE(
            "label",
            offsetof(struct lgtd_jsonrpc_target_args, label),
            -1,
            // this must dereference json from the what's in the token (see
            // next comment):
            lgtd_jsonrpc_type_number,
            false
        )
    };
开发者ID:aoighost,项目名称:lightsd,代码行数:33,代码来源:test_jsonrpc_extract_values_from_schema_and_array_honors_objsize.c


示例12: parse_json_file

/* Parses the given JSON File 
 * Returns
 *  - 0 on success and all the arguments will be pointing to parsed data
 *  - 1 on failure and garbage elsewhere
 */
int parse_json_file(const char *filename, int *arg_c, int *live, char *user, char *group, char *command) {
  int fd;			/* file descriptor */
  int r = 1;			/* result (default, failure) */
  char json_str[FILE_SIZE+10];	/* file content store */

  /* slurp the file contents */
  fd = open(filename, R_OK);
  if (fd == -1) {
    _LOGGER("(ERROR) Cannot Open File [%s] Reason [%s]", filename, strerror(errno));
    return 1;
  }

  r = read(fd, json_str, FILE_SIZE); /* i should use stat to see file size, this is a possible overflow */
  if (r <= 0 || r >= FILE_SIZE) {
    _LOGGER("File size for file (%s) unexpected expected size (Expected Size:%d)", filename, FILE_SIZE);
    return 1;
  }
  json_str[r] = '\0';		/* more efficient than bzero :-) */

  close(fd);

  r = parse_json(json_str, arg_c, live, user, group, command);

  return r;
}
开发者ID:cloud-hot,项目名称:marionette,代码行数:30,代码来源:json_parser.c


示例13: memset

json_t *json_load_callback(json_load_callback_t callback, void *arg, size_t flags, json_error_t *error)
{
    lex_t lex;
    json_t *result;

    callback_data_t stream_data;

    memset(&stream_data, 0, sizeof(stream_data));
    stream_data.callback = callback;
    stream_data.arg = arg;

    jsonp_error_init(error, "<callback>");

    if (callback == NULL) {
        error_set(error, NULL, "wrong arguments");
        return NULL;
    }

    if(lex_init(&lex, (get_func)callback_get, &stream_data))
        return NULL;

    result = parse_json(&lex, flags, error);

    lex_close(&lex);
    return result;
}
开发者ID:cryptobiote,项目名称:jansson,代码行数:26,代码来源:load.c


示例14: jsonp_error_init

json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)
{
    lex_t lex;
    const char *source;
    json_t *result;
    (void)flags; /* unused */

    if(lex_init(&lex, (get_func)fgetc, input))
        return NULL;

    if(input == stdin)
        source = "<stdin>";
    else
        source = "<stream>";

    jsonp_error_init(error, source);

    result = parse_json(&lex, error);
    if(!result)
        goto out;

    lex_scan(&lex, error);
    if(lex.token != TOKEN_EOF) {
        error_set(error, &lex, "end of file expected");
        json_decref(result);
        result = NULL;
    }

out:
    lex_close(&lex);
    return result;
}
开发者ID:derdewey,项目名称:jansson,代码行数:32,代码来源:load.c


示例15: main

int
main(void)
{
    jsmntok_t tokens[32];
    const char json[] = ("{"
        "\"jsonrpc\": \"2.0\","
        "\"method\": \"power_off\","
        "\"params\": {\"target\": \"*\"},"
        "\"id\": \"42\""
    "}");
    int parsed = parse_json(
        tokens, LGTD_ARRAY_SIZE(tokens), json, sizeof(json)
    );

    bool ok;
    struct lgtd_jsonrpc_request req = TEST_REQUEST_INITIALIZER;
    struct lgtd_client client = {
        .io = NULL, .current_request = &req, .json = json
    };
    ok = lgtd_jsonrpc_check_and_extract_request(&req, tokens, parsed, json);
    if (!ok) {
        errx(1, "can't parse request");
    }

    lgtd_jsonrpc_check_and_call_power_off(&client);

    if (!power_off_called) {
        errx(1, "lgtd_proto_power_off wasn't called");
    }

    return 0;
}
开发者ID:rivy,项目名称:lightsd,代码行数:32,代码来源:test_jsonrpc_check_and_call_power_off.c


示例16: save_json

static int save_json(const struct mg_str *data, const char *file_name) {
  FILE *fp;
  int len = parse_json(data->p, data->len, NULL, 0);
  if (len <= 0) {
    LOG(LL_ERROR, ("%s\n", "Invalid JSON string"));
    return 0;
  }
  fp = fopen("tmp", "w");
  if (fp == NULL) {
    LOG(LL_ERROR, ("Error opening file for writing\n"));
    return 0;
  }
  if (fwrite(data->p, 1, len, fp) != (size_t) len) {
    LOG(LL_ERROR, ("Error writing file\n"));
    fclose(fp);
    return 0;
  }
  if (fclose(fp) != 0) {
    LOG(LL_ERROR, ("Error closing file\n"));
    return 0;
  }
  if (rename("tmp", file_name) != 0) {
    LOG(LL_ERROR, ("Error renaming file to %s\n", file_name));
    return 0;
  }
  return 1;
}
开发者ID:apmascalabs,项目名称:mongoose-iot,代码行数:27,代码来源:device_config.c


示例17: get_weather_all_city

/*可以根据用户的要求获取某地的天气信息,如输入“大连”或“dalian”,可以查询大连的天气信息
 *该函数获取一天的信息。
 *get_weather_all_city2()可以获取六天的天气信息。main函数中直接调用这两个函数即可得到天气信息。
*/
void get_weather_all_city()
{
   char city_name[MAX]; 
   memset(&city_name, 0, sizeof(city_name));
   printf("Input city name:");
   fgets(city_name, MAX, stdin);
   //printf("weather:%s \n", city_name);
   city_name[strlen(city_name) - 1] = '\0';
   char *city_name1 = (char *)malloc(MAX);
   strcpy(city_name1, city_name);
   //printf("city_name1: %s\n", city_name1);
   //char *file_name = (char *)malloc(MAX);
   char *file_name = strcat(city_name1, ".cache");
   //printf(" filename:%s\n", file_name);
   char *city_id_json = http_request(city_name, "/search?cityname=", "61.4.185.213", 80);
   //printf("%s\n",city_id_json);
   char *cityid;
   cityid = extract_city_id(city_id_json);
   char *ss = strcat(cityid, ".html");
   
   char *city_weather_info = http_request(ss, "/data/cityinfo/", "61.4.185.201", 80);
   //char *city_weather_info = http_request(ss, "/data/", "113.108.239.118 ", 80);  //or IP:113.108.239.107
   //printf("city_weather_info\n%s\n", city_weather_info);

   char *city_weather_json = extract_weather_json(city_weather_info, 8);
   //printf("city_weather_json\n%s\n",city_weather_json);
   char *weather = parse_json(city_weather_json);
   printf("%s\n", weather);
  

}
开发者ID:liyangddd,项目名称:network_information,代码行数:35,代码来源:weather.c


示例18: osd_opendir

void plugin_options::parse_json(std::string path)
{
	// first try to open as a directory
	osd_directory *directory = osd_opendir(path.c_str());
	if (directory != nullptr)
	{
		// iterate over all files in the directory
		for (const osd_directory_entry *entry = osd_readdir(directory); entry != nullptr; entry = osd_readdir(directory))
		{
			if (entry->type == ENTTYPE_FILE)
			{
				std::string name = entry->name;
				if (name == "plugin.json")
				{
					std::string curfile = std::string(path).append(PATH_SEPARATOR).append(entry->name);
					std::ifstream ifs(curfile);
					rapidjson::IStreamWrapper isw(ifs);
					rapidjson::Document document;
					document.ParseStream<0>(isw);

					if (document.HasParseError()) {
						std::string error(GetParseError_En(document.GetParseError()));
						osd_printf_error("Unable to parse plugin definition file %s. Errors returned:\n", curfile.c_str());
						osd_printf_error("%s\n", error.c_str());
						return;
					}

					if (document["plugin"].IsObject())
					{
						std::string name = document["plugin"]["name"].GetString();
						std::string description = document["plugin"]["description"].GetString();
						std::string type = document["plugin"]["type"].GetString();
						bool start = false;
						if (document["plugin"].HasMember("start") && (std::string(document["plugin"]["start"].GetString()) == "true"))
							start = true;

						if (type=="plugin")
						{
							add_entry(core_strdup(name.c_str()),core_strdup(description.c_str()), OPTION_BOOLEAN, start ? "1" : "0");
						}
					}

				}
			}
			else if (entry->type == ENTTYPE_DIR)
			{
				std::string name = entry->name;
				if (!(name == "." || name == ".."))
				{
					parse_json(path + PATH_SEPARATOR + name);
				}
			}
		}

		// close the directory and be done
		osd_closedir(directory);
	}
}
开发者ID:RJRetro,项目名称:mame,代码行数:58,代码来源:pluginopts.cpp


示例19: iio_ioctl_json

int32_t 
iio_ioctl_json(void *dev_handle, uint32_t opcode, char *injson,
               char **outjson, void *ctx_out, uint32_t flags)
{
    struct iio_device *device = (struct iio_device *)dev_handle;
    struct qnio_msg *msg = NULL;
    struct iovec data, out;
    kvset_t *inps = NULL;
    kvset_t *outps = NULL;
    qnio_stream *stream = NULL;
    int err;

    if (injson != NULL) {
        inps = parse_json(injson);
        if(inps == NULL) {
            nioDbg("Parse json failed");
            return -1;
        }
    }

    msg = iio_message_alloc(&apictx->msg_pool);
    msg->hinfo.opcode = opcode;
    msg->hinfo.data_type = DATA_TYPE_PS;
    msg->hinfo.payload_size = 0;
    data.iov_len = 0;
    if (inps != NULL) {
        msg->send = new_io_vector(1, NULL);
        data.iov_base = kvset_marshal(inps, (int *)&(data.iov_len));
        io_vector_pushback(msg->send, data);
        kvset_free(inps);
    }
    msg->recv = NULL;
    msg->hinfo.payload_size = data.iov_len;
    safe_strncpy(msg->hinfo.target, device->devid, NAME_SZ64);
    msg->user_ctx = ctx_out;
    msg->hinfo.flags = QNIO_FLAG_REQ_NEED_RESP;
    err = iio_msg_submit(device, msg, flags);
    if (err) {
        iio_message_free(msg);
    } else if(!(flags & IIO_FLAG_ASYNC)) {
        err = iio_msg_wait(msg);
        if(err == 0) {
            if (msg->recv) {
                out = io_vector_at(msg->recv, 0);
                stream = new_qnio_stream(0);
                kvset_unmarshal(out.iov_base, &outps);
                kvset_print(stream, 0, outps); 
                *outjson = (char *) malloc(stream->size + 1);
                memcpy(*outjson, stream->buffer, stream->size);
		        ((char *)*outjson)[stream->size] = '\0';
                qnio_delete_stream(stream);
                kvset_free(outps);
            }
        }
        iio_message_free(msg);
    }
    return err;
}
开发者ID:MittalAshish,项目名称:libqnio,代码行数:58,代码来源:iioapi.c


示例20: process_repl_commands

/**
 * @name process_repl_commands:
 */
void process_repl_commands(gammu_state_t **s, FILE *stream) {

  for (;;) {

    boolean_t is_eof = FALSE;
    char *line = read_line(stream, &is_eof);

    if (!line) {
      break;
    }

    if (is_eof && line[0] == '\0') {
      goto cleanup;
    }

    parsed_json_t *p = parse_json(line);

    if (p) {

      char **argv = NULL;
      int argc = 0, err = 0;

      boolean_t rv = parsed_json_to_arguments(p, &argc, &argv, &err);

      if (!rv) {
        print_json_validation_error(err);
        goto cleanup_json;
      }

      int result = 0;

      if (!process_command(s, argc, argv, &result)) {

        if (!result) {
          print_usage_error(U_ERR_CMD_INVAL);
        }
        goto cleanup_json;
      }

    } else {
      print_json_validation_error(V_ERR_PARSE);
    }

    cleanup_json:

      if (p) {
        release_parsed_json(p);
      }

    cleanup:

      free(line);

      if (is_eof) {
        break;
      }
  }
}
开发者ID:hack-alex,项目名称:gammu-json,代码行数:61,代码来源:gammu-json.c



注:本文中的parse_json函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C++ parse_line函数代码示例发布时间:2022-05-30
下一篇:
C++ parse_integer函数代码示例发布时间:2022-05-30
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap