/* 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库函数使用的更多相关文章

  1. Sword pcre库函数学习三

    14.pcre_get_substring_list 原型: #include <pcre.h> int pcre_get_substring_list(const char *subje ...

  2. Sword pcre库函数学习二

    9.pcre_free_substring_list 原型: #include <pcre.h> void pcre_free_substring_list(const char **st ...

  3. Sword pcre库函数学习一

    0.pcre_exec 原型: #include <pcre.h> int pcre_exec(const pcre *code, const pcre_extra *extra, con ...

  4. cJSON学习笔记

    1.JSON格式简述 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写,同时也易于机器解析和生成.它基于JavaScript(Standa ...

  5. cJSON库的简单介绍及使用

    转载:http://www.cnblogs.com/liunianshiwei/p/6087596.html JSON 语法是 JavaScript 对象表示法语法的子集.数据在键/值对中:数据由逗号 ...

  6. Entity Framework 6 Recipes 2nd Edition(11-11)译 -> 在LINQ中调用数据库函数

    11-11. 在LINQ中调用数据库函数 问题 相要在一个LINQ 查询中调用数据库函数. 解决方案 假设有一个任命(Appointment )实体模型,如Figure 11-11.所示, 我们想要查 ...

  7. Linux系统调用和库函数调用的区别

    Linux下对文件操作有两种方式:系统调用(system call)和库函数调用(Library functions).系统调用实际上就是指最底层的一个调用,在linux程序设计里面就是底层调用的意思 ...

  8. C标准I/O库函数与Unbuffered I/O函数

    一.C标准I/O库函数.Unbuffered I/O函数 1. C标准I/O库函数是如何用系统调用的 fopen(3) 调用open(2)打开制定的文件,返回一个文件描述符(一个int类型的编号),分 ...

  9. [Django]模型提高部分--聚合(group by)和条件表达式+数据库函数

    前言:本文以学习记录的形式发表出来,前段时间苦于照模型聚合中group by 找了很久,官方文章中没有很明确的说出group by,但在文档中有提到!!! 正文(最后编辑于2016-11-12): 聚 ...

随机推荐

  1. vue 博客知识点汇总

    1. vue修改url,页面不刷新 项目中经常会用到同一个页面,结构是相同的,我只是在vue-router中通过添加参数的方式来区分状态,参数可以在页面跳转时带上params,或者query,但是有一 ...

  2. Handling skewed data---Error metrics for skewed(偏斜的) classes(precision&recall)

    skewed classes skewed classes: 一种类里面的数量远远高于(或低于)另一个类,即两个极端的情况. 预测cancer的分类模型,如果在test set上只有1%的分类误差的话 ...

  3. Windows窗体控件实现内容拖放(DragDrop)功能

    一.将控件内容拖到其他控件 在开发过程中,经常会有这样的要求,拖动一个控件的数据到另外一个控件中.例如将其中一个ListBox中的数据拖到另一个ListBox中.或者将DataGridView中的数据 ...

  4. Django --- 多对多关系创建,forms组件

    目录 多对多三种创建方式 1.系统直接创建 2.自己手动创建 3.自己定义加与系统创建 forms组件 1. 如何使用forms组件 2. 使用forms组件校验数据 3. 使用forms组件渲染标签 ...

  5. springboot开发人员工具(自动重启及相关的配置)

    导入依赖: <dependencies> <dependency> <groupId>org.springframework.boot</groupId> ...

  6. IList<> IEnumerable<> ReadOnlyCollection<> 使用方向

    无论你把它声明为IList<string>,IEnumerable<string>,ReadOnlyCollection<string>或别的东西,是你...... ...

  7. mysql 运算操作符

    Name Description AND, && Logical AND = Assign a value (as part of a SET statement, or as par ...

  8. Ubuntu shell系统的环境变量

    1.系统环境变量env命令查看 1)利用export命令导出环境变量 export PS1 导出PS1 添加路径 export PATH=$PATH:/home/daokr/myfile $ sudo ...

  9. leetcode解题报告(31):Kth Largest Element in an Array

    描述 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...

  10. js中array.some()的用法

    let array=[ { name:'jack', age:'19' }, { name:'rose', age:'19' } ] var box=array.some((value,index)= ...