jansson 示例

安装jansson
./configure
make
make install
生成帮助文档
cd doc
make html
编译选项需要添加“-ljansson”

jansson 手册:

https://jansson.readthedocs.io/en/latest/index.html

API 介绍:

 http://jansson.readthedocs.io/en/2.2/apiref.html#json_error_t

创建json对象与导出:

#include <stdio.h>
#include <jansson.h>
#include <stdint.h>
#include <string.h>

int main(int argc, char **argv) {
    //#构造 json数据
    json_t *rootT = json_object();
    json_t *item = json_object();
    json_object_set_new(item, "name", json_string("xiaochen"));
    json_object_set_new(item, "age", json_integer(22));
    /* json_object_set(rootT, "root", item);
    json_object_set会增加item对象的引用计数,一般用于添加到另一个json对象上。*/
    json_object_set_new(rootT, "root", item);

    // json对象导出,转换成字符串。
    char *pStrRoot = json_dumps(rootT, 0);
    json_delete(rootT);
    printf("pStrRoot:%s\n", pStrRoot);

    json_error_t error;
    json_t* root = json_object();
    json_t* jsonArgsObj = json_loads(pStrRoot, 0, &error);//反序列化

    json_object_set_new(root, "code", json_integer(0));
    json_object_set_new(root, "message", json_string("strMessage"));
    json_object_set_new(root, "sdk", json_string("strApiName"));
    json_object_set_new(root, "args", jsonArgsObj);
//    json_object_set_new(root, "args", json_string(pStrRoot));
//    json_object_set_new(root, "data", jsonDataObj);
//    json_decref(jsonArgsObj);

    char *json = json_dumps(root, JSON_INDENT(0));  //序列化
    printf("json:%s\n", json);
    free(pStrRoot);
    // json_dumps返回的对象要手动释放。
    free(json);

    // test get json object from one node of a json object
    json_t *js_args = json_object();
    json_t *js_item = json_object_get(root, "args");
    if(!json_is_object(js_item))
    {
        printf("js not a object.\n");
        return 0;
    }
    /*js_item对象来自root,将其添加到另外一个对象时需要使用json_object_set增加其引用计数。
    此处不可使用json_object_set_new。*/
    //json_object_set_new(js_args, "newArgs", js_item);
    json_object_set(js_args, "newArgs", js_item);
    json = json_dumps(js_args, JSON_INDENT(0));  //序列化
    printf("json:%s\n", json);
    free(json);
//    json_object_del(js_args, "newArgs");
//    json_object_clear(js_args);
    json_decref(js_args);

//    json_delete(root);
    json_decref(root);
//    printf("jsonArgsObj result:%p\n", jsonArgsObj);
//    if(jsonArgsObj)
//    {
////        json_decref(jsonArgsObj);
////        jsonArgsObj = NULL;
//    }
//    printf("again jsonArgsObj result:%p\n", jsonArgsObj);
    return 0;
}

 

 

 

string object array error number

#include <stdio.h>
#include <stdlib.h>
#include <stdfix.h>
#include <string.h>
#include <iostream>
#include <jansson.h>
#include <jansson_config.h>

using namespace std;

int main() {
    json_t val;
    json_type eval = JSON_REAL;
    bool Judge = 1;
    int type = json_typeof(&val); // Get the type of Obj
    Judge = json_is_object(&val); // Obj Judge
    cout << "json_is_object:" << Judge << endl;
    Judge = json_is_array(&val); //Object judged to be array
    cout << "Json_is_array:" << Judge << endl;
    cout << "The type of val:" << type << endl;

    /* JSON array Defination and value setting */
    json_t *array, *integer;
    array = json_array(); // Create the json array
    integer = json_integer(42); // Create the json integer
    json_array_append(array,integer); // Set the value of json array
    json_decref(integer); // Drop the content of integer and can no longer be used.

    /* JSON-STRING */
    json_auto_t *value = NULL;
    value = json_string("Hello"); // Initial a new string json obj
    json_string_set(value, "World!"); // Use set func to change the content of json string obj.
    size_t len = json_string_length(value); // Get the length of the json string obj.
    cout << "The len of value:" << len << endl;

    /* JSON-NUMBER */
    json_int_t number; // Define a json type number.
    number = 100; // initial the number.
    printf("number is %" JSON_INTEGER_FORMAT "\n",number);
    number = json_integer_value(integer); // Get the json type integer's value to number.
    printf("value of integer(json_t) is %" JSON_INTEGER_FORMAT "\n",number); // The integer had been decrefed in line27.(So it's zero)
    integer = json_real(3.1415926); // Set a real number value to integer.
    double res = json_real_value(integer); // Read out the value to a double type var.(The integer can store the double type json data.)
    printf("The res is:%f\n",res);

    /* JSON-ARRAY */
    json_t *Matrix = json_array(); // json array defination.
    json_array_append(Matrix,integer); // Add a integer obj to the array.
    size_t Matrix_Size = json_array_size(Matrix); // Test the size of the array after add a new integer obj to it.
    printf("The size of Matrix:%d\n",Matrix_Size);

    /* JSON-OBJECT */
    json_t *obj = json_object(); // Define a new json obj.
    size_t obj_size = json_object_size(obj); // Get the size of the json obj.
    printf("The size of obj:%d\n",obj_size);
    char *key = "Hello";
    integer = json_integer(52); // Set the value of integer json obj.
    int back = json_object_set(obj,key,integer); // Setup a new obj named "Hello" and the mapped value is 52.
    json_t *read_num_back = json_object_get(obj,key); // Get back the obj named "Hello".
    int num_val = json_integer_value(read_num_back);  // Get the value of the Hello obj.
    cout << "The value of integer:" << num_val << endl; // Echo out the Value.

    while(1);
    return 0;
}

 

 

#include<stdio.h>
#include<string.h>
#include<jansson.h>

#define FILE_PATH         "./temp.txt"
#define MAX_NUM            5

typedef struct _JSON_ITEM_INFO
{
    json_t* string;
    json_t* value;
}JSON_ITEM_INFO;


void save_info_to_file()
{
   json_t* root = NULL;
   json_t* item_1 = NULL;
   json_t* item_2 = NULL;
   json_t* item_3 = NULL;
   json_t* array = NULL;

   char* s_repon = NULL;

   root = json_object();
   item_1 = json_object();
   item_2 = json_object();
   item_3 = json_object();
   array = json_array();

   json_object_set_new(item_1,"name",json_string("xiaopeng"));
   json_object_set_new(item_1,"age",json_integer(12));
   json_array_append_new(array,item_1);

   json_object_set_new(item_2,"name",json_string("xiaoming"));
   json_object_set_new(item_2,"age",json_integer(8));
   json_array_append_new(array,item_2);

   json_object_set_new(item_3,"name",json_string("xiaohong"));
   json_object_set_new(item_3,"age",json_integer(22));
   json_array_append_new(array,item_3);

   json_object_set_new(root,"root",array);

   json_dump_file(root, FILE_PATH,JSON_PRESERVE_ORDER);

   s_repon = json_dumps(root,JSON_INDENT(0));

   printf("s_repon = %s \n",s_repon);
   free(s_repon);

   printf("size = %d \n", (int)json_array_size(array));

   if(root)
   {
      json_delete(root);
   }
   if(array)
   {
      json_delete(array);
   }
}

void get_file_info()
{
   int i = 0;

   json_t* root = NULL;
   json_t* array = NULL;
   json_error_t error;
   char* s_repon = NULL;

   json_t* add_item_1 = NULL;
   char* s_get_add_item = NULL;

   json_t* rec_table[MAX_NUM] = {0};

   JSON_ITEM_INFO person[MAX_NUM];
   memset(person,0,sizeof(person));

   //get the info from file;
   root = json_load_file(FILE_PATH, 0, &error);
   if(!json_is_object(root))
   {
        printf("%s,%d\n",__FILE__,__LINE__);
   }
   s_repon = json_dumps(root,JSON_INDENT(0));
   printf("s_repon = %s \n",s_repon);
   free(s_repon);

   array = json_object_get(root,"root");
   if(!json_is_array(array))
   {
        printf("%s,%d\n",__FILE__,__LINE__);
   }

   for(i = 0; i < MAX_NUM ;i++)
   {
       rec_table[i] = json_array_get(array,i);
       if(!json_is_object(rec_table[i]))
       {
            printf("%s,%d\n",__FILE__,__LINE__);
       }
       person[i].string = json_object_get(rec_table[i],"name");
       printf("person[%d].string = %s \n",i,json_string_value(person[i].string));
       person[i].value = json_object_get(rec_table[i],"age");
       printf("person[%d].value = %d \n",i,(int)json_integer_value(person[i].value));
   }

    //add the new item;
    add_item_1 = json_object();
    json_object_set_new(add_item_1,"name",json_string("zhangsan"));
    json_object_set_new(add_item_1,"age",json_integer(30));

   if(json_array_size(array) >= MAX_NUM)
   {
        //remove the top item;
        json_array_remove(array,0);

   }
    json_array_append_new(array,add_item_1);

    //write the new array to the file;
    json_dump_file(root, FILE_PATH,JSON_PRESERVE_ORDER);

    //dump the date and print
    s_get_add_item = json_dumps(root,JSON_INDENT(0));

    printf("s_get_add_item = %s \n",s_get_add_item);
    free(s_get_add_item);

}

int main()
{
    save_info_to_file(); //这里将数据保存在文件 FILE_PATH 里;
    get_file_info();     // 这里将文件 FILE_PATH 数据读取出来;

    return 0;
}

 

编译:

gcc source.c -ljansson

 

Jansson库的使用简介
JSON 下 -- jansson 示例
posted @ 2022-08-24 19:59  PKICA  阅读(410)  评论(1编辑  收藏  举报