首页 > 其他分享 > jansson 示例

jansson 示例

时间:2022-08-24 20:14:23浏览次数:128  
标签:item object 示例 jansson json new array root

安装jansson 步骤:

http://blog.csdn.net/lz909/article/details/46042979

jansson 手册:

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

API 介绍:

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

 

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 示例

标签:item,object,示例,jansson,json,new,array,root
From: https://www.cnblogs.com/guxuanqing/p/16621394.html

相关文章