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): 聚 ...
随机推荐
- 零基础Python教程-详说list有序集合
list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出你周围同事的名字,就可以用一个list表示: >>> classmates = ['Michael', 'Bob' ...
- #《你们都是魔鬼吗》第八次团队作业:第五天Alpha冲刺
<你们都是魔鬼吗>第八次团队作业:Alpha冲刺 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 你们都是魔鬼吗 作业学习目标 完成最 ...
- go http编程
http的请求包包含 请求行,请求头,空行,请求体go的http编程 http server.go package main import "net/http" func main ...
- stm32自带的flash分布图
缘由是要用到flash来保存数据,因此查阅了数据手册与参考手册,一般情况下,将要保存的数据存放到比较靠后的地方,page254,page255,4k字节,已经相当多的了,
- 使用SAXReader对XML进行操作
该例子主要使用SAXReader对XML进行操作,browse.xml是Ango框架里面的XML文件 采用两种方法,第一种的全部是iterator,另外一种采用了部分的for each 代码如下 pr ...
- 08 node.js 的使用
创建包 目录结构 cmd cd 到当前目录: \ 执行 npm init //创建一个包 1 2. 3. 4.包的安装 npm install jquery --save npm install ...
- Spark 的两种核心 Shuffle (HashShuffle 与 与 SortShuffle) 的 的工作流程
1. 参考博客:https://blog.csdn.net/qichangjian/article/details/88039576
- 5、Hadoop 2.6.5 环境搭建
下载 地址:http://archive.apache.org/dist/hadoop/common/ sudo wget http://archive.apache.org/dist/hadoop/ ...
- am335x system upgrade kernel can(八)
1 Scope of Document This document describes can bus hardware design and can bus driver developm ...
- CSS渐变色边框,解决border设置渐变后,border-radius无效的问题
需求:用css设置渐变边框通过border-image来实现渐变色边框 <div class="content"></div> .content { wid ...