Sword cjson库函数使用
/* cjson库的使用 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h> #include "cJSON.h" /*
说明:
组装成json效率并不高,并不推荐json,字符串远比json快,但是字符串表示不了对象,protobuf虽然快,但是依赖于第三方库,很棘手
*/ //数据解析
int testPause(const char * pcJson)
{
cJSON *pstRoot = NULL;
cJSON *pstNode = NULL;
cJSON *pstFeat = NULL;
cJSON *pstArray = NULL;
cJSON *pstAudio = NULL;
cJSON *pstRole = NULL;
cJSON *pstTmp = NULL;
int arraySize = ;
int i = , j = ;
int featSize = ; if (NULL == pcJson)
{
return -;
} //解析json字符串
pstRoot = cJSON_Parse(pcJson);
if (NULL == pstRoot)
{
return -;
} do
{
//获取json对象
pstAudio = cJSON_GetObjectItem(pstRoot, "audio");
if (NULL == pstAudio)
{
printf("--no audio info .---\n");
break;
} //获取字符串型json对象的值
printf("====audio info %s=======\n", pstAudio->valuestring); pstArray = cJSON_GetObjectItem(pstRoot, "array");
if (NULL == pstArray)
{
printf("--no array info .---\n");
break;
} //提取json数组
//1.获取数组长度
arraySize = cJSON_GetArraySize(pstArray);
if ( == arraySize)
{
printf("--no array info .---\n");
break;
} for (i = ; i < arraySize; i++)
{
pstNode = cJSON_GetArrayItem(pstArray, i);
if (pstNode)
{
//获取角色信息
pstRole = cJSON_GetObjectItem(pstNode, "role");
if (NULL == pstRole)
{
printf("--no role info .---\n");
break;
}
printf("====role info %d=======\n", pstRole->valueint); //获取声纹信息
pstFeat = cJSON_GetObjectItem(pstNode, "feat");
if (NULL == pstFeat)
{
printf("--no feat info .---\n");
break;
} //获取声纹长度
featSize = cJSON_GetArraySize(pstFeat);
if ( == featSize)
{
printf("--no feat size info .---\n");
break;
}
for (j = ; j < featSize; j++)
{
pstTmp = cJSON_GetArrayItem(pstFeat, j);
printf("==feat[%lf]==\n", pstTmp->valuedouble);
}
}
} } while (); // 释放资源
if (pstRoot)
{
cJSON_Delete(pstRoot);
pstRoot = NULL;
} return ;
} //数据组装
char * testAssemble(void)
{
cJSON *pstRoot = NULL;
cJSON *pstNode = NULL;
cJSON *pstFeat = NULL;
cJSON *pstArray = NULL;
float aFeat[] = { 1.1f, 2.2f, 3.3f, 4.4f, 5.5f, 6.6f, 7.7f, 8.8f, 9.9f, 0.0f };
char * pOutput = NULL; //创建普通json对象
pstRoot = cJSON_CreateObject();
assert(pstRoot); pstNode = cJSON_CreateObject();
assert(pstNode); //创建float数组类型json对象
pstFeat = cJSON_CreateFloatArray(aFeat, );
assert(pstFeat); cJSON_AddNumberToObject(pstNode, "role", );
cJSON_AddItemToObject(pstNode, "feat", pstFeat); //创建一个json对象数组
pstArray = cJSON_CreateArray();
assert(pstArray); //将一个json对象加入到json数组中
cJSON_AddItemToArray(pstArray, pstNode); //布尔值添加
cJSON_AddStringToObject(pstRoot, "audio", "test.wav");
cJSON_AddItemToObject(pstRoot, "array", pstArray); //输出json字符串(内存需要自己释放)
pOutput = cJSON_Print(pstRoot); // 释放资源
if (pstRoot)
{
cJSON_Delete(pstRoot);
pstRoot = NULL;
} return pOutput; } int main()
{
char * pcJson = NULL; pcJson = testAssemble();
if (NULL == pcJson)
{
printf("----[testAssemble]-----failed-----\n");
return -;
} //打印数据
printf("===%s==\n", pcJson); printf("\n"); //解析json
testPause(pcJson); //释放资源
if (pcJson)
{
free(pcJson);
pcJson = NULL;
} printf("-----ok------\n"); getchar(); return ;
}
Sword cjson库函数使用的更多相关文章
- Sword pcre库函数学习三
14.pcre_get_substring_list 原型: #include <pcre.h> int pcre_get_substring_list(const char *subje ...
- Sword pcre库函数学习二
9.pcre_free_substring_list 原型: #include <pcre.h> void pcre_free_substring_list(const char **st ...
- Sword pcre库函数学习一
0.pcre_exec 原型: #include <pcre.h> int pcre_exec(const pcre *code, const pcre_extra *extra, con ...
- cJSON学习笔记
1.JSON格式简述 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写,同时也易于机器解析和生成.它基于JavaScript(Standa ...
- cJSON库的简单介绍及使用
转载:http://www.cnblogs.com/liunianshiwei/p/6087596.html JSON 语法是 JavaScript 对象表示法语法的子集.数据在键/值对中:数据由逗号 ...
- Entity Framework 6 Recipes 2nd Edition(11-11)译 -> 在LINQ中调用数据库函数
11-11. 在LINQ中调用数据库函数 问题 相要在一个LINQ 查询中调用数据库函数. 解决方案 假设有一个任命(Appointment )实体模型,如Figure 11-11.所示, 我们想要查 ...
- Linux系统调用和库函数调用的区别
Linux下对文件操作有两种方式:系统调用(system call)和库函数调用(Library functions).系统调用实际上就是指最底层的一个调用,在linux程序设计里面就是底层调用的意思 ...
- C标准I/O库函数与Unbuffered I/O函数
一.C标准I/O库函数.Unbuffered I/O函数 1. C标准I/O库函数是如何用系统调用的 fopen(3) 调用open(2)打开制定的文件,返回一个文件描述符(一个int类型的编号),分 ...
- [Django]模型提高部分--聚合(group by)和条件表达式+数据库函数
前言:本文以学习记录的形式发表出来,前段时间苦于照模型聚合中group by 找了很久,官方文章中没有很明确的说出group by,但在文档中有提到!!! 正文(最后编辑于2016-11-12): 聚 ...
随机推荐
- lomback插件在日志管理方面的应用
由于现在使用日志可以省去在解决bug时候的很多麻烦, lomback为我们提供了很方便的打印日志的管理 @RunWith(SpringRunner.class) @SpringBootTest @Sl ...
- idea的基础设置
1. 在idea.exe.vmoptions文件里,如图进行修改即可,, 优化的原理就是调用更多的运行内存来加载他,所以4G的话本身会不够,就会造成其他应用很卡. 2. 下面是进行一些基本的配置 ...
- AOP与Filter拦截请求打印日志实用例子
相信各位同道在写代码的时候,肯定会写一些日志打印,因为这对往后的运维而言,至关重要的. 那么我们请求一个restfull接口的时候,哪些信息是应该被日志记录的呢? 以下做了一个基本的简单例子,这里只是 ...
- Spring源码窥探之:BeanPostProcessor
Spring的Bean后置处理器 1. 实体类 /** * @author 70KG * @Title: Train * @Description: * @date 2018/7/23下午11:31 ...
- mui真机调试时无法查找到手机
在做mui的真机调试时手机查找不到手机,有可能是你的 adb interface 驱动 版本过低,先下载 或自行百度 http://www.onlinedown.net/soft/1164054. ...
- PostgreSQL 查看表、索引等创建时间
select s.oid,s.relname,t.stausename,t.stasubtype from pg_class s,pg_stat_last_operation t where s.re ...
- YAML_14 tags给指定的任务定义一个调用标识,以后不用重复整个过程,只需要执行tags标签的部分
ansible]# vim adhttp.yml --- - hosts: cache remote_user: root tasks: - copy: src: /r ...
- BZOJ 5093: [Lydsy1711月赛]图的价值 第二类斯特林数+NTT
定义有向图的价值为图中每一个点的度数的 \(k\) 次方之和. 求:对于 \(n\) 个点的无向图所有可能情况的图的价值之和. 遇到这种题,八成是每个点单独算贡献,然后累加起来. 我们可以枚举一个点的 ...
- AtCoder Grand Contest 039 题解
传送门 \(A\) 首先只有一串的情况下,遇到相同的肯定是改后面那一个最优,然后两串的话可能要分奇偶讨论一下 //quming #include<bits/stdc++.h> #defin ...
- 获取句柄的类型以及对应的ID序号
遍历所有进程下的所有句柄,以及对应句柄类型. 一丶简介 在有的时候.我们会需要对应句柄名字.以及句柄类型的名称. 以及它所对应的的ID. 因为每个系统不一样.所以每次都是不一样的. 有的时候我们就需要 ...