2021-07-20 C语言 反解析 json字符

/*
  • Tests if casting within the json_object_get_* functions work correctly.

  • Also checks the json_object_get_type and json_object_is_type functions.
    */
    //gcc -o test test_cast.c -I /usr/local/include/json-c/ -L /usr/local/bin/ -l json-c
    //FOR ARM
    // C R O S S C O M P I L E g c c − o c p u i d g e t s . c / / − L / u s r / l o c a l / j s o n − c − 0.9 a r m / l i b / / / {CROSS_COMPILE}gcc -o cpuid gets.c //-L /usr/local/json-c-0.9arm/lib/ // CROSSCOMPILEgccocpuidgets.c//L/usr/local/jsonc0.9arm/lib///{CROSS_COMPILE}gcc -ljson -I /usr/local/json-c-13arm/include/json/ -L /usr/local/json-c-13arm/lib/ -o test test2.c

    #include <stdio.h>
    

    #include <string.h>
    #include <stdlib.h>

#include “config.h”
#include “json_inttypes.h”
#include “json_object.h”
#include “json_tokener.h”
#include “json_util.h”

static int getit(struct json_object *new_obj, const char *field);
static int checktype_header(void);
static int checktype(struct json_object *new_obj, const char *field);

int main(int argc, char **argv)
{
const char input = “{\n
“string_of_char”: “testjson”,\n
“string_of_digits”: “123”,\n
“regular_number”: 222,\n
“decimal_number”: 99.55,\n
“boolean_true”: true,\n
“boolean_false”: false,\n
“big_number”: 2147483649,\n
“a_null”: null,\n
}”;
/
Note: 2147483649 = INT_MAX + 2 */

//申请容器
struct json_object *new_obj;

//json 字符放入容器
new_obj = json_tokener_parse(input);

// printf(“Parsed input: %s\n”, input);
// printf(“Result is %s\n”, (new_obj == NULL) ? “NULL (error!)” : “not NULL”);
//注入字符失败
if (!new_obj)
return 1; // oops, we failed.

//
char *t_1 = "string_of_digits";

//按项反转获取
//1.new_obj 字符数组 
//2.The second param name
//getit(new_obj, "string_of_digits");
	/*	\"string_of_char\": \"testjson\",\n\ */
getit(new_obj, "string_of_char");		
getit(new_obj, t_1);	
getit(new_obj, "regular_number");
getit(new_obj, "decimal_number");
getit(new_obj, "boolean_true");
getit(new_obj, "boolean_false");
getit(new_obj, "big_number");
getit(new_obj, "a_null");

// Now check the behaviour of the json_object_is_type() function.
// 检测行为的 json_object_is_type 功能

// char *t_2 = “string_of_digits”;
// printf("\n================================\n");
// checktype_header(); //检测类型
// checktype(new_obj, NULL); //空
//checktype(new_obj, “string_of_digits”);
// checktype(new_obj, t_2);
// checktype(new_obj, “regular_number”);
// checktype(new_obj, “decimal_number”);
// checktype(new_obj, “boolean_true”);
// checktype(new_obj, “boolean_false”);
// checktype(new_obj, “big_number”);
// checktype(new_obj, “a_null”);

json_object_put(new_obj);

return 0;

}

static int getit(struct json_object *new_obj, const char *field)
{
struct json_object *o = NULL;
if (!json_object_object_get_ex(new_obj, field, &o))
printf(“Field %s does not exist\n”, field);

enum json_type o_type = json_object_get_type(o);
//手动注入类型 并输出

// printf(“new_obj.%s json_object_get_type()=%s\n”, field,
// json_type_to_name(o_type)); //打印类型
printf(“new_obj.%s json_object_get_int()=%d\n”, field,
json_object_get_int(o)); //输出 int 类型
// printf(“new_obj.%s json_object_get_int64()=%” PRId64 “\n”, field,
// json_object_get_int64(o)); //输出 int64 类型
// printf(“new_obj.%s json_object_get_boolean()=%d\n”, field,
// json_object_get_boolean(o));
// printf(“new_obj.%s json_object_get_double()=%f\n”, field,
// json_object_get_double(o));

return 0;

}

static int checktype_header()
{
// printf(“json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n”,
// json_type_to_name(json_type_null),
// json_type_to_name(json_type_boolean),
// json_type_to_name(json_type_double),
// json_type_to_name(json_type_int),
// json_type_to_name(json_type_object),
// json_type_to_name(json_type_array),
// json_type_to_name(json_type_string));

return 0;

}
static int checktype(struct json_object *new_obj, const char *field)
{
struct json_object *o = new_obj;
if (field && !json_object_object_get_ex(new_obj, field, &o))
printf(“Field %s does not exist\n”, field);

// printf(“new_obj%s%-18s: %d,%d,%d,%d,%d,%d,%d\n”,
// field ? “.” : " ", field ? field : “”,
// json_object_is_type(o, json_type_null),
// json_object_is_type(o, json_type_boolean),
// json_object_is_type(o, json_type_double),
// json_object_is_type(o, json_type_int),
// json_object_is_type(o, json_type_object),
// json_object_is_type(o, json_type_array),
// json_object_is_type(o, json_type_string));

return 0;	

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值