将JSON字符串放入C中的数据库中

时间:2011-10-13 22:34:18

标签: c json function pointers berkeley-db

以下是我的代码:

#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字符串:{“Extracurricular”:“5,6,7,41,40,22,9,12,91,97”} type:type:json_type_string           价值:5,6,7,41,40,22,9,12,91,97 db:10:密钥存储。 db:10:检索到的密钥:数据是p4È@4ȸ4È500

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

1 个答案:

答案 0 :(得分:1)

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