• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
cunning007
博客园    首页    新随笔    联系   管理    订阅  订阅

cJson简单入门

1.JSON 是什么:一种数据交互格式,与protobuf有一定相同之处 ,cJSON是实现这种格式转换接口的C库

1. cJSON 库下载 : https://sourceforge.net/projects/cjson/files/latest/download

2.JSON的合法符号:{(左大括号)      }(右大括号)     ""(双引号)    ,(逗号)    :(冒号)        [ (左中括号)      ] (右中括号)

3. JSON字符串:特殊字符可在字符前面加\或使用\u加四位16进制数来处理   { "name" : "LiMing" }

4.JSON 布尔(必须小写的true或false){ "bool" : true }

5.JSON空(必须小写null){ "object":null }

6. JSON数组 (不能8/16进制数){"num":1} {"num":-1} {"num":1.1}

7.JSON对象

  {"data" :

    {

    "name":"a",

    "sex":"m"

    }

  }

8.JSON数组

  {

    "data":

    [

      "name",

      10  

    ]

  }

 

9.JSON对象数组

{
    "array": [
        {
            "name": "jobs"
        },
        {
            "name": "bill",
            "age": 60
        },
        {
            "product": "war3",
            "type": "game",
            "popular": true,
            "price": 60
        }
    ]
}

 

10.简单编程

typedef struct
{
	s32t len;
	u8t c[][CJSON_FIELED_BUFSIZE];
}CjsonField , *CjsonFieldPtr;

typedef struct
{
	cJSON *root_t;
	u8t *proot_t;
	CjsonFieldPtr cfproot_t;
}CjsonManage;

CjsonFieldPtr myjson_init(s32t cj_num)
{
	int n_t = 0;

	CjsonFieldPtr cfield = (CjsonFieldPtr)malloc(sizeof(CjsonField) + cj_num * CJSON_FIELED_BUFSIZE);

	if(cfield == NULL)
	{
		printf("cjsonfield malloc failed!\n");
		return NULL;
	}

//	memcpy(cfield->c[0] , "name", CJSON_FIELED_BUFSIZE);
	snprintf(cfield->c[0] , CJSON_FIELED_BUFSIZE , "%s" , "name");
	snprintf(cfield->c[1] , CJSON_FIELED_BUFSIZE , "%s" , "sex");
	snprintf(cfield->c[2] , CJSON_FIELED_BUFSIZE , "%s" , "age");

#if defined (PRINT_TEST)
	for(n_t = 0 ; n_t < cj_num ; n_t++)
	{
		printf("cfield_[%d] = %s\n" , n_t , cfield->c[n_t]);
	}
#endif

	cfield->len = cj_num;

	return cfield;
}

s32t myjson_delete(CjsonFieldPtr p)
{
	if(NULL == p)
		return -1;
	free(p);

	return 0;
}

CjsonManage makejson(s32t num_t)
{
	CjsonFieldPtr json = NULL;
	cJSON *root = NULL;
	char * proot = NULL;
	CjsonManage cjm = {NULL,NULL,NULL};


	json = myjson_init(num_t);
	cjm.cfproot_t = json;
	if(NULL == json)
	{
		printf("myjson_init failed!\n");
	}

	root = cJSON_CreateObject();
	cjm.root_t = root;
	if(NULL == root)
	{
		printf("cJSON_CreateObject failed!\n");
		myjson_delete(json);
	}

	cJSON_AddStringToObject(root , json->c[0] , "LiMing");
	cJSON_AddStringToObject(root , json->c[1] , "man");
	cJSON_AddNumberToObject(root , json->c[2] , 16);

	proot = cJSON_Print(root);
	cjm.proot_t = (u8t*)proot;
	if(proot == NULL)
	{
		printf("cJSON_Print failed!\n");
	}

#if defined (PRINT_TEST)
		printf("JSON: %s\n" ,proot);
#endif

	return cjm;
}


s32t parseJson(CjsonManage cjm_t)
{
	cJSON *root = NULL;

	root = cJSON_Parse(cjm_t.proot_t);
	if(root == NULL)
	{
		return -1;
	}

	cJSON *name = cJSON_GetObjectItem(root , cjm_t.cfproot_t->c[0]);
	printf("name:%s\n" , name->valuestring);
	cJSON *sex = cJSON_GetObjectItem(root , cjm_t.cfproot_t->c[1]);
	printf("sex:%s\n" , sex->valuestring);
	cJSON *age = cJSON_GetObjectItem(root , cjm_t.cfproot_t->c[2]);
	printf("age:%d\n" , age->valueint);

	free(cjm_t.proot_t);
	free(cjm_t.cfproot_t);
	cJSON_Delete(cjm_t.root_t);

	return 0;
}

 

 

以上,

2018-8-29

posted @ 2018-08-29 16:46  cunning007  阅读(263)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3