阿里 lite_cjson使用方法入门

void test_json(void)
{
    int res = 0,i;
    char *test_str ="{\"a\":1,\"b\":2.5,\"c\":\"jxd\"}";
    lite_cjson_t lite, lite_item;
    lite_cjson_item_t *obj,*one_item;
    char *json_txt;
    lite_cjson_hooks hooks;
    char tmp_buf[20]={0};
    double test_double;
 /// //创建一个cjson对象,填充对象,并将对象数据打印出来
    memset(&lite, 0, sizeof(lite_cjson_t));
    printf("start test function test_json 3\r\n");
    hooks.free_fn = HAL_Free;
    hooks.malloc_fn = HAL_Malloc;
    json_txt = HAL_Malloc(80);
    lite_cjson_init_hooks(&hooks);
   obj = lite_cjson_create_object();
   printf("lite_cjson_create_object ok \r\n");
     if(obj)
    {
        //lite_cjson_add_null_to_object(obj, "a");
        lite_cjson_add_true_to_object(obj, "b");
        printf("lite_cjson_add_true_to_object ok \r\n");
       /* lite_cjson_add_false_to_object(obj, "c");
        printf("lite_cjson_add_false_to_object ok \r\n");
        lite_cjson_add_bool_to_object(obj, "d", 0);
        printf("lite_cjson_add_bool_to_object ok \r\n");
        lite_cjson_add_bool_to_object(obj, "e", 1);
        printf("lite_cjson_add_bool_to_object ok \r\n");
        lite_cjson_add_number_to_object(obj, "f", 22);
        printf("lite_cjson_add_number_to_object ok \r\n")*/;
       /* test_double = 2.287;
        lite_cjson_add_number_to_object(obj, "g",test_double);
        printf("lite_cjson_add_number_to_object ok \r\n");
        lite_cjson_add_string_to_object(obj, "h", "jxd_test");
        printf("lite_cjson_add_string_to_object ok \r\n");
        one_item = obj->child;
        if(one_item == 0)
        {
            printf("lite_cjson_add_number_to_object faile \r\n");
        }
        else
        {
            printf("one_item->string = %s\r\n", one_item->string);
            printf("one_item->valuestring = %s\r\n", one_item->valuestring);
            printf("one_item->valuedouble = %d\r\n", one_item->valuedouble*1000);
            printf("one_item->valueint = %d\r\n", one_item->valueint);
            test_double = test_double*1000;
            printf("test_double = %d\r\n", test_double);
            
        }*/

        json_txt = lite_cjson_print(obj);
        printf("lite_cjson_print ok  \r\n");
    }

/创建cjson对象完毕

 

//根据cjson格式文本字符串创建对象,并打印每一个item

    //res = lite_cjson_parse(test_str, strlen(test_str), &lite);
    res = lite_cjson_parse(json_txt, strlen(json_txt), &lite);
    printf("lite_cjson_parse ok \r\n");
    if (res != 0) {
        memset(&lite, 0, sizeof(lite_cjson_t));
        printf("test_json creat lite fail\r\n");
        return ;
    }
    printf("lite.type = %d\r\n", lite.type);
    printf("lite.value = %s\r\n", lite.value);
    printf("lite.value_length = %d\r\n", lite.value_length);
    printf("lite.size = %d\r\n", lite.size);
    printf("lite.value_double = %f\r\n", lite.value_double);
    printf("lite.value_int = %d\r\n", lite.value_int);
     printf("\r\n"); printf("\r\n");
   // if(lite.type == cJSON_Object)
    {
        for(i = 'a'; i < 'i'; i++)
        {
            memset(&lite_item, 0, sizeof(lite_cjson_t));
            res = lite_cjson_object_item(&lite, (const char *)&i, 1, &lite_item);
            if(res != 0) continue;
            printf("lite_item.type = %d\r\n", lite_item.type);
            printf("lite_item.value = %s\r\n", lite_item.value);
            printf("lite_item.value_length = %d\r\n", lite_item.value_length);
            printf("lite_item.size = %d\r\n", lite_item.size);
            printf("lite_item.value_double = %f\r\n", lite_item.value_double);
            printf("lite_item.value_int = %d\r\n", lite_item.value_int);
            printf("\r\n"); printf("\r\n");
        }
    }
   HAL_Free(json_txt);
   lite_cjson_delete(obj);
  end//
   printf("end test function test_json\r\n");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值