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): 聚 ...
随机推荐
- go cache
go 编译, 或是安装库的时候,产生的日志量很大 go env 删除掉这个log.txt文件,系统空间瞬间就够了
- php 实现密码错误三次锁定账号10分钟
/** * 登录 * 1.接收数据 * 2.正则判断接收到的数据是否合理 * 3.根据用户名获取用户数据 * 获取到数据 -> 继续执行 * 没有获取到数据 -> 提示:用户名密码错误 * ...
- Spring源码窥探之:FactoryBean
1. 定义Fish实体类 /** * @author 70KG * @Title: Fish * @Description: * @date 2018/7/22下午5:00 * @From www.n ...
- Oracle-分析函数之取上下行数据lag()和lead()
这两个函数是偏移量函数,可以查出一个字段的上一个值或者下一个值,配合over来使用. lead函数,这个函数是向上偏移. lag函数是向下偏移一位. 语法 [语法] lag(EXPR,<OFFS ...
- Tensorflow细节-P112-模型持久化
第一个代码 import tensorflow as tf v1 = tf.Variable(tf.random_normal([1], stddev=1, seed=1)) v2 = tf.Vari ...
- ArrayList 集合的几种遍历的方法
ArrayList 集合 也可称作动态数组(长度可变),在新建的时候是没有默认长度的,在新增数据长度小于10的时候,ArrayList 的长度会自动设置为10 //了解更多可以按住Ctrl 再点击你 ...
- greenplum常见问题及解决方法
本文链接:https://blog.csdn.net/q936889811/article/details/85612046 文章目录 1.错误:数据库初始化:gpini ...
- Linux 命令-egrep
egrep 可以看着是grep的扩展,参数-e支持正则匹配 满足其中任一条件 egrep -e "tiaojian1|tiaojian2" test.txt
- Spring入门(四)——整合Mybatis
1. 准备jar包及目录结构 2. 配置db.properties driver = com.mysql.jdbc.Driver url = jdbc:mysql://127.0.0.1:3306/H ...
- .net大文件分块上传断点续传demo
IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag 客户端每次提交下载请求时,服务 ...