gpt4 book ai didi

c - 用 C 将 JSON 字符串放入数据库

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

下面是我的代码:

#include <json/json.h>
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <db.h>

#define DATABASE "access.db"


/*printing the value corresponding to boolean, double, integer and strings*/
void print_json_value(json_object *jobj){
enum json_type type;
printf("type: ",type);
type = json_object_get_type(jobj); /*Getting the type of the json object*/
switch (type) {
case json_type_boolean: printf("json_type_boolean\n");
printf("value: %s\n", json_object_get_boolean(jobj)? "true": "false");
break;
case json_type_double: printf("json_type_double\n");
printf(" value: %lf\n", json_object_get_double(jobj));
break;
case json_type_int: printf("json_type_int\n");
printf(" value: %d\n", json_object_get_int(jobj));
break;
case json_type_string: printf("json_type_string\n");
printf(" value: %s\n", json_object_get_string(jobj));
break;
}

}


/*Parsing the json object*/
void json_parse(json_object * jobj) {
enum json_type type;
json_object_object_foreach(jobj, key, val) { /*Passing through every array element*/
printf("type: ",type);
type = json_object_get_type(val);
switch (type) {
case json_type_boolean:
case json_type_double:
case json_type_int:
case json_type_string: print_json_value(val);
db_json(&val);
break;
case json_type_object: printf("json_type_object\n");
jobj = json_object_object_get(jobj, key);
json_parse(jobj);
break;
case json_type_array: printf("type: json_type_array, ");
json_parse_array(jobj, key);
break;
}
}
}

int db_json(char *val) {

typedef struct {

char data1[500];

} pearson_record;

pearson_record s;


int i =0;
DB *dbp;
DBT key, data;
int ret, t_ret;
int recno;



if ((ret = db_create(&dbp, NULL, 0)) != 0) {
fprintf(stderr, "db_create: %s\n", db_strerror(ret));
exit (1);
}

// if ((ret = dbp->set_flags(dbp, DB_RECNUM)) != 0) {
// fprintf(stderr, "db_create: %s\n", db_strerror(ret));
// exit (1);
// }

if ((ret = dbp->open(dbp,
NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0) {
dbp->err(dbp, ret, "%s", DATABASE);
goto err;
}

strncpy(s.data1, val, strlen(val)+1);

//printf("chk %\n",jvalue);

recno = 10;

memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
//memset(&s, 0, sizeof(struct pearson_record));
key.data = &recno;
key.size = sizeof(recno);
data.data = &s;
data.size = sizeof(s);


if ((ret = dbp->put(dbp, NULL, &key,&data,0)) == 0)
printf("db: %d: key stored.\n", *(int *)key.data);
else
{
dbp->err(dbp, ret, "DB->put");
goto err;
}

pearson_record *ppr;
if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0) {

ppr = (pearson_record *) data.data;

printf("db: %d: key retrieved: data was %s %d\n",
*(int *)key.data, ppr->data1, data.size);
}
else {
dbp->err(dbp, ret, "DB->get");
goto err;
}

err: if ((t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
ret = t_ret;

exit(ret);
}

int main() {

char * string = "{\"Extracurricular\" : \"5,6,7,41,40,22,9,12,91,97\"}";
printf("JSON string: %s\n", string);
json_object * jobj = json_tokener_parse(string);
json_parse(jobj);
}

当我编译代码时,我看到以下警告:

try.c:81:8:警告:函数“db_json”的隐式声明

我的输出是:JSON字符串:{“课外”:“5,6,7,41,40,22,9,12,91,97”}类型: 类型:json_type_string 值:5、6、7、41、40、22、9、12、91、97db: 10: 存储 key 。db: 10: 检索到 key :数据为 p4È@4ȸ4È 500

基本上我想将 JSON 字符串的值传递到 Berkeley DB 数据库中。我无法确定将值传递给我的函数是否存在问题,或者是否还有其他问题。我确信我在指针和函数中犯了一些小问题。任何形式的帮助将不胜感激。提前致谢。

最佳答案

一个问题是 db_json 函数接受 char* 作为参数,但对它的调用似乎是将指针的地址传递给 >json_object。我不熟悉这些函数,但您可能应该将 json_object_get_string(jobj) 作为参数传递给 db_json,因为这似乎会产生一个字符串数据的 JSON 表示形式。请注意,这只是通过简要查看代码得出的猜测;我对涉及的类型不太熟悉。

关于c - 用 C 将 JSON 字符串放入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7761222/

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