c语言中如何使用json

json: 为了避免不同平台下的字节对齐、类型大小不统一的问题,json库把数据封装成具有一定格式的字符流数据,进行传输。

 

json格式:把数据与键值一一对应,数据传输双方约定好同一键值,

使用接口API根据键值操作jason对象(jason_object)存储或取得数据。

一般使用:

数据-》(封装)jason对象-》String格式-》。。。传输。。。-》String格式-》(解析)jason对象-》取得数据

(int、char..)数据,与键值成对存入json对象—————————————————————————————————>通过键值从json对象取得数据

 

 

json接口API(注意:在json中所有数据类型(arry、int、string、char)都是一个jason对象)

 

1、数据的封装(单对象(int、char、string)和数组(arry))

(1)新建对象:

A.创建一个Json对象:

struct json_object * json_object_new_object (void)

 

B.创建一个Json数组对象:

struct json_object * json_object_new_array (void)

 

C.销毁json对象

void json_object_put (struct json_object *obj)

 

(2)json对象的转换(普通类型->json对象):

1:struct json_object * json_object_new_int (int i)

2:struct json_object * json_object_new_double (double d)

3:struct json_object * json_object_new_string (const char *s)

4:struct json_object * json_object_new_boolean (boolean b)

5:struct json_object * json_object_new_string_len (const char *s, int len)

 

(3)json对象的处理

A.普通对象

添加:void json_object_object_add (struct json_object *obj, const char *key, struct json_object *val)

删除:void json_object_object_del (struct json_object *obj, const char *key)

查询:struct json_object * json_object_object_get (struct json_object *obj, const char *key)

根据key获取:struct json_object * json_object_object_get (struct json_object *obj, const char *key)

 

B.数组对象

获取长度:int json_object_array_length (struct json_object *obj)

添加:int json_object_array_add (struct json_object *obj, struct json_object *val)

指定位置添加:int json_object_array_put_idx (struct json_object *obj, int idx, struct json_object *val)

获取指定位置对象:struct json_object * json_object_array_get_idx (struct json_object *obj, int idx)

 

(4)json_object To 字符流

const char * json_object_to_json_string (struct json_object *obj)

 

1、数据的解析(解析获取到的json格式字符流)

(1)字符流 To json_object

struct json_object* json_tokener_parse(const char *str)

 

(2)对象获取

A.普通对象

根据key获取:struct json_object * json_object_object_get (struct json_object *obj, const char *key)

 

B.数组对象

获取指定位置对象:struct json_object * json_object_array_get_idx (struct json_object *obj, int idx)

 

(3)对象的转换(数据还原)

bool型:boolean json_object_get_boolean (struct json_object *obj)

double型:double json_object_get_double (struct json_object *obj)

整型:int json_object_get_int (struct json_object *obj)

字符数组:const char * json_object_get_string (struct json_object *obj)

//创建一个JSON对象
json_object* json_object_new_object(void);

二、将普通数据转为json对象
//把整型转换到JSON类型
json_object* json_object_new_int(int n);
//把字符串转换到JSON类型
json_object* json_object_new_string(char *buf);


三、将json对象转换为普通数据
//把JSON对象转换成字符串
char* json_object_to_json_string(json_object* data);


/*------------------------------------------分割线----------------------------------------*/


一、创建(数据的打包)

1、定义big_object结构体存放整个json包:

     struct json_object *big_object=json_object_new_object();

2、往big_object中添加一级的K-V对:

      json_object_object_add(big_object, "text", json_object_new_string("MXCHIP won a prize"));

3、定义detail_object存放二级嵌套的数据:

      struct json_object *detail_object = json_object_new_object();

4、往detail_object中添加K-V对:

      json_object_object_add(detail_object, "comp", json_object_new_string("MXCHIP.Inc"));

5、创建数组存放module对应的value:

     struct json_object *myarray = json_object_new_array( );

6、分别创建四个json_object结构体存放数组内的K-V对,再依次添加到数组myarray中

      struct json_object *k=json_object_new_object();;

      json_object_object_add(k, "k", json_object_new_string("EMW3165"));

      json_object_array_add(myarray,k);

7、将myarray添加到detail_object中,再将detail_object添加到big_object中:

      json_object_object_add(detail_object, "module", myarray);

      json_object_object_add(big_object, "detail", detail_object);

至此,整个json数据包打包完成!

逻辑还是很简单的,可以将{"k":"value"}看成json数据包的基本格式,然后从基本K-V对组成module->detail_object->big_object。

8、将big_object对象转字符串,就可以打印出来了:

     char *string = json_object_to_json_string(big_object);

二、解析(数据的解包)

1、先把字符串转成对象,定义一个parse_object接收

     json_object *parse_object= json_tokener_parse(string);

2、把一级结构下的K-V对遍历解析出来

     json_object_object_foreach(parse_object, key, val)

3、创建一个嵌套对象temp_object,用来接收detail

     json_object* temp_object=json_object_object_get(parse_object,"detail");

4、将detail下的前三个K-V对分别接收,调用的是: json_object_object_get。

     再调用 json_object_to_json_string将对象转成字符串打印出来。

5、最后来处理detail下的module数组,首先定义一个数组用来接收module

     json_object* array= json_object_object_get(temp_object,"module");

     然后在循坏内将module内的K-V对一个个接收出来

     for(int i=0; i

     struct json_object *obj =json_object_array_get_idx(array, i);

     struct json_object *value_object=json_object_object_get(obj,"k");

     os_json_log("k=%s",json_object_to_json_string(value_object));

   }


 

使用示范

#include 
#include 
#include 
#include 
#include 
int main(void)
{
      //JSONd打包
      struct json_object *my_json,*my_id,*my_name;
      //创建一个JSON对象
      my_json=json_object_new_object();
      //把整型转换到JSON类型
      my_id=json_object_new_int(1);
      my_name=json_object_new_string("猪肉炒牛肉");
      //添加到JSON对象
      json_object_object_add(my_json,"id",my_id);
      json_object_object_add(my_json,"name",my_name);
      //把JSON对象转换成字符串
     const char * my_json_str=json_object_to_json_string(my_json);
       printf("%s\n",my_json_str);
      
      struct json_object *my_json2,*my_id2,*my_name2;
      //创建一个JSON对象
      my_json2=json_object_new_object();
      //把整型转换到JSON类型
      my_id2=json_object_new_int(2);
      my_name2=json_object_new_string("青椒炒红椒");
      //添加到JSON对象
      json_object_object_add(my_json2,"id",my_id2);
      json_object_object_add(my_json2,"name",my_name2);
      //把JSON对象转换成字符串
     const char * my_json_str2=json_object_to_json_string(my_json2);
       printf("%s\n",my_json_str2);

       struct json_object *json_shuzu;
       //创建一个JSON数组对象
        json_shuzu=json_object_new_array();
      //把两个JSON对象加到JSON数组
	json_object_array_add(json_shuzu,my_json);
	json_object_array_add(json_shuzu,my_json2);

     const char * my_json_str3=json_object_to_json_string(json_shuzu);
       printf("%s\n",my_json_str3);

      
      //JSON解包
       json_object *jiebao_json,*jiebao_name;
      //把字符串转换成JSON对象
       jiebao_json=json_tokener_parse(my_json_str);
       //通过键值获取JSON成员
        jiebao_name=json_object_object_get(jiebao_json,"name");
	//还原
	  const char *jiebao_str=json_object_get_string(jiebao_name);
	 printf("%s\n",jiebao_str);


      //JSON解包
       json_object *jiebao_json2, *jiebao_json3,*jiebao_name2;
       jiebao_json2=json_tokener_parse(my_json_str3);

	jiebao_json3=json_object_array_get_idx(jiebao_json2,0);
        jiebao_name2=json_object_object_get(jiebao_json3,"name");

	  const char *jiebao_str2=json_object_get_string(jiebao_name2);
	 printf("--------%s\n",jiebao_str2);
      return 0;
}

封装示范

#include "head.h"

/*
 *功能:将消息类型对象与消息对象通过json转化为字符串地址
 *a:消息类型对象
 *b: 消息对象
 *返回值:字符串指针地址
 */

const char *json_arr(struct json_object *a,struct json_object *b)
{
	struct json_object *json_shuzu;
	//创建一个JSON数组对象
	json_shuzu=json_object_new_array();
	//把两个JSON对象加到JSON数组
	if(b == NULL)
	{
		json_object_array_add(json_shuzu,a);
		const char *my_json_str3=json_object_to_json_string(json_shuzu);
		return my_json_str3;
	}
	json_object_array_add(json_shuzu,a);
	json_object_array_add(json_shuzu,b);

	const char *my_json_str3=json_object_to_json_string(json_shuzu);

	return my_json_str3;
}


/*
 *功能:将消息类型结构体地址通过json转化为字符串地址
 *head:消息类型结构体地址
 *返回值:字符串指针地址
 */
struct json_object *json_msg(msg_plist head)
{
    //JSONd打包
    struct json_object *my_json,*my_msg;
    //创建一个JSON对象
    my_json=json_object_new_object();
    //把整型转换到JSON类型
    my_msg=json_object_new_int(head->msg);
    //添加到JSON对象
    json_object_object_add(my_json,"msg",my_msg);
	
    return my_json;
}

/*
 *功能:将菜单结构体地址通过json转化为字符串地址
 *head:菜单结构体地址
 *返回值:字符串指针地址
 */
struct json_object *json_menu(menu_plist head)
{
    //JSONd打包
    struct json_object *my_json,*my_dishid,*my_dishname,*my_price,*my_storage;
    //创建一个JSON对象
    my_json=json_object_new_object();
    //把整型转换到JSON类型
    my_dishid=json_object_new_int(head->dishid);
    my_dishname=json_object_new_string(head->dishname);
    my_price=json_object_new_double(head->price);
    my_storage=json_object_new_int(head->storage);
    //添加到JSON对象
    json_object_object_add(my_json,"dishid",my_dishid);
    json_object_object_add(my_json,"dishname",my_dishname);
    json_object_object_add(my_json,"price",my_price);
    json_object_object_add(my_json,"storage",my_storage);

    return my_json;
}

/*
 *功能:将订单结构体地址通过json转化为字符串地址
 *head:订单结构体地址
 *返回值:字符串地址
 */
struct json_object *json_order(order_plist head)
{
    //JSONd打包
    struct json_object *my_json,*my_orderid,*my_dishname,*my_uname,*my_price,*my_status;
    //创建一个JSON对象
    my_json=json_object_new_object();
    //把整型转换到JSON类型
    my_orderid=json_object_new_int(head->orderid);
    my_dishname=json_object_new_string(head->dishname);
    my_uname=json_object_new_string(head->uname);
    my_price=json_object_new_double(head->price);
    my_status=json_object_new_string(head->status);
    //添加到JSON对象
    json_object_object_add(my_json,"orderid",my_orderid);
    json_object_object_add(my_json,"dishname",my_dishname);
    json_object_object_add(my_json,"uname",my_uname);
    json_object_object_add(my_json,"price",my_price);
    json_object_object_add(my_json,"status",my_status);

    return my_json;
}


/*
 *功能:将用户结构体地址通过json转化为字符串地址
 *head:用户结构体地址
 *返回值:字符串地址
 */
struct json_object *json_user(user_plist head)
{

    //JSONd打包
    struct json_object *my_json,*my_uid,*my_uname,*my_tableid,*my_type;
    //创建一个JSON对象
    my_json=json_object_new_object();
    //把整型转换到JSON类型
	my_type=json_object_new_int(head->type);
    my_uid=json_object_new_int(head->uid);
    my_uname=json_object_new_string(head->uname);
    my_tableid=json_object_new_int(head->tableid);
    //添加到JSON对象
	json_object_object_add(my_json,"type",my_type);
    json_object_object_add(my_json,"uid",my_uid);
    json_object_object_add(my_json,"uname",my_uname);
    json_object_object_add(my_json,"tableid",my_tableid);

    return my_json;
}

/*
 *功能:将字符串地址转化为消息结构体地址
 *p:字符串地址
 *返回值:消息结构体地址
 */
msg_plist json_xmsg(json_object *jiebao_json)
{
    //JSON解包
    json_object *jiebao_msg;
    msg_plist st;
    st = (msg_plist)malloc(sizeof(msg_list));
    if(st == NULL)
    {
        perror("json_menu_malloc failed");
        exit(1);
    }
	
    //通过键值获取JSON成员
    jiebao_msg=json_object_object_get(jiebao_json,"msg");
    //还原
    const char *jiebao_str1=json_object_get_string(jiebao_msg);

    sscanf(jiebao_str1,"%d",&st->msg);
	
    return st;
}

/*
 *功能:将字符串地址转化为菜单结构体地址
 *p:字符串地址
 *返回值:菜单结构体地址
 */
menu_plist json_xmenu(json_object *jiebao_json)
{
    //JSON解包
    json_object *jiebao_dishname,*jiebao_dishid,*jiebao_price,*jiebao_storage;
    menu_plist st;
    st = (menu_plist)malloc(sizeof(menu_list));
    if(st == NULL)
    {
        perror("json_menu_malloc failed");
        exit(1);
    }
	
    //通过键值获取JSON成员
    jiebao_dishid=json_object_object_get(jiebao_json,"dishid");
    jiebao_dishname=json_object_object_get(jiebao_json,"dishname");
    jiebao_price=json_object_object_get(jiebao_json,"price");
    jiebao_storage=json_object_object_get(jiebao_json,"storage");
    //还原
    const char *jiebao_str1=json_object_get_string(jiebao_dishid);
    const char *jiebao_str2=json_object_get_string(jiebao_dishname);
    const char *jiebao_str3=json_object_get_string(jiebao_price);
    const char *jiebao_str4=json_object_get_string(jiebao_storage);

    sscanf(jiebao_str1,"%d",&st->dishid);
    sscanf(jiebao_str2,"%s",st->dishname);
    sscanf(jiebao_str3,"%lf",&st->price);
    sscanf(jiebao_str4,"%d",&st->storage);

    return st;
}


/*
 *功能:将字符串地址转化订单结构体地址
 *p:字符串地址
 *返回值:订单结构体地址
 */
order_plist json_xorder(json_object *jiebao_json)
{
    //JSON解包
    json_object *jiebao_dishname,*jiebao_orderid,*jiebao_price,*jiebao_status,*jiebao_uname;;
    order_plist st;
    st = (order_plist)malloc(sizeof(order_list));
    if(st == NULL)
    {
        perror("json_order_malloc failed");
        exit(1);
    }

    //通过键值获取JSON成员
    jiebao_orderid=json_object_object_get(jiebao_json,"orderid");
    jiebao_dishname=json_object_object_get(jiebao_json,"dishname");
    jiebao_price=json_object_object_get(jiebao_json,"price");
    jiebao_uname=json_object_object_get(jiebao_json,"status");
    jiebao_status=json_object_object_get(jiebao_json,"uname");
    //还原
    const char *jiebao_str1=json_object_get_string(jiebao_orderid);
    const char *jiebao_str2=json_object_get_string(jiebao_dishname);
    const char *jiebao_str3=json_object_get_string(jiebao_price);
    const char *jiebao_str4=json_object_get_string(jiebao_status);
    const char *jiebao_str5=json_object_get_string(jiebao_uname);

    sscanf(jiebao_str1,"%d",&st->orderid);
    sscanf(jiebao_str2,"%s",st->dishname);
    sscanf(jiebao_str3,"%lf",&st->price);
    sscanf(jiebao_str4,"%s",st->status);
    sscanf(jiebao_str5,"%s",st->uname);

    return st;

}


/*
 *功能:将字符串地址转化用户结构体地址
 *p:字符串地址
 *返回值:用户结构体地址
 */
user_plist json_xuser(json_object *jiebao_json)
{
    //JSON解包
    json_object *jiebao_uname,*jiebao_uid,*jiebao_tableid,*jiebao_type;
    user_plist st;
    st = (user_plist)malloc(sizeof(user_list));
    if(st == NULL)
    {
        perror("json_user_malloc failed");
        exit(1);
    }

    //通过键值获取JSON成员
	jiebao_type=json_object_object_get(jiebao_json,"type");
    jiebao_uid=json_object_object_get(jiebao_json,"uid");
    jiebao_uname=json_object_object_get(jiebao_json,"uname");
    jiebao_tableid=json_object_object_get(jiebao_json,"tableid");
    //还原
	const char *jiebao_str4=json_object_get_string(jiebao_type);
    const char *jiebao_str1=json_object_get_string(jiebao_uid);
    const char *jiebao_str2=json_object_get_string(jiebao_uname);
    const char *jiebao_str3=json_object_get_string(jiebao_tableid);

    sscanf(jiebao_str1,"%d",&st->uid);
    sscanf(jiebao_str2,"%s",st->uname);
    sscanf(jiebao_str3,"%d",&st->tableid);
	sscanf(jiebao_str4,"%d",&st->type);
    return st;

}

/*
 *功能:字符串地址转换为具体的结构体地址
 *p:字符串地址
 *a:0为第一个数组解析,1为第二个数组解析;
 *b:0为解析消息类型,1为解析菜单数据;2为解析订单数据,3解析用户数据.
 *返回值:通用结构体指地址
 */

void *json_xarr(const char *p,int a,int b)
{
	//JSON解包
	msg_plist ms;
	menu_plist me;
	order_plist or;
	user_plist us;
	json_object *jiebao_json2, *jiebao_json3,*jiebao_name2;
	jiebao_json2=json_tokener_parse(p);

	jiebao_json3=json_object_array_get_idx(jiebao_json2,a);
	if(b == 0)
	{
		ms =json_xmsg(jiebao_json3);
		return (void *)ms;
	}	
	else if(b == 1)
	{
		me = json_xmenu(jiebao_json3);
		return (void *)me;
	}
	else if(b == 2)
	{
		or = json_xorder(jiebao_json3);
		return (void *)or;
	}
	else if(b == 3)
	{	
		us = json_xuser(jiebao_json3);
		return (void *)us;
	}
	else
	{
		perror("注册信息错误\n");
		exit(1);
	}
}

 

 

你可能感兴趣的:(嵌入式)