【cJson】cJSON的构造和解析(五)

2024-08-31 09:08
文章标签 cjson 解析 构造

本文主要是介绍【cJson】cJSON的构造和解析(五),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 对于cJSON的使用,我主要是用来模拟远程服务器端返回的一个json类型的目录结构,客户端进行获取并进行解析,把解析出来的目录按照原本的结构显示在本地。

cJSON是一个超轻巧,携带方便,单文件,简单的可以作为ANSI-C标准的JSON解析器。

进入cJSON.h头文件中可以查看cJSON的相关信息。主要包括:cJSON结构体、cJSON类型、cJSON的一些内部的函数等。

// cJSON结构体:

typedef struct cJSON {

     struct cJSON *next,*prev;   // next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem

     struct cJSON *child;        // An array or object item will have a child pointer pointing to a chain of the items in the array/object.

     int type;                   // The type of the item, as above.

     char *valuestring;          // The item's string, if type==cJSON_String

     int valueint;               // The item's number, if type==cJSON_Number

     double valuedouble;         // The item's number, if type==cJSON_Number

     char *string;               // The item's name string, if this item is the child of, or is in the list of subitems of an object.

} cJSON;

 

// cJSON 类型:

#define cJSON_False 0

#define cJSON_True 1

#define cJSON_NULL 2

#define cJSON_Number 3

#define cJSON_String 4

#define cJSON_Array 5

#define cJSON_Object 6

 

用法:

1、需要包含cJSON.h头文件,然后和cJSON.c或库文件libcJSON.a一起编译即可使用。

2、具体函数用法详见cJSON.h注释

更多介绍机器使用请参考:http://sourceforge.net/projects/cjson/.

cJSON构造与解析json结构体

基本代码如下:

#include

#include

#include

#include "cJSON.h"

char * create1()

{

     cJSON *root,*dir1,*dir2,*dir3;

     char *out;

     //创建json数组型结构体

     root = cJSON_CreateArray();

     //为数组添加对象

     cJSON_AddItemToArray(root,dir1=cJSON_CreateObject());

     //为对象添加字符串键值对

     cJSON_AddStringToObject(dir1,"name",".");

     cJSON_AddStringToObject(dir1,"path","uploads/");

     cJSON_AddStringToObject(dir1,"flag","true");

     cJSON_AddItemToArray(root,dir2=cJSON_CreateObject());

     cJSON_AddStringToObject(dir2,"name","..");

     cJSON_AddStringToObject(dir2,"path","uploads");

     cJSON_AddStringToObject(dir2,"flag","true");

     cJSON_AddItemToArray(root,dir3=cJSON_CreateObject());

     cJSON_AddStringToObject(dir3,"name","wang.txt");

     cJSON_AddStringToObject(dir3,"path","uploads/wang.txt");

     cJSON_AddStringToObject(dir3,"flag","false");

     //将json结构体转换为字符串

     out=cJSON_Print(root);

     //删除

     cJSON_Delete(root);

     return out;

}

 

char * create2()

{

     cJSON *root,*dir,*child,*subdir,*dir1,*dir2,*dir3;

     char *out;

     root=cJSON_CreateObject();

    

     cJSON_AddItemToObject(root,"Root",dir=cJSON_CreateObject());

     cJSON_AddStringToObject(dir,"name","/");

     cJSON_AddStringToObject(dir,"path","/");

     cJSON_AddStringToObject(dir,"flag","true");

 

     cJSON_AddItemToObject(root,"Child",subdir = cJSON_CreateArray());

     cJSON_AddItemToObject(subdir,"dira",dir1=cJSON_CreateObject());

     cJSON_AddStringToObject(dir1,"name",".");

     cJSON_AddStringToObject(dir1,"path","/./");

     cJSON_AddStringToObject(dir1,"flag","true");

     cJSON_AddItemToObject(subdir,"dira",dir2=cJSON_CreateObject());

     cJSON_AddStringToObject(dir2,"name","..");

     cJSON_AddStringToObject(dir2,"path","/../");

     cJSON_AddStringToObject(dir2,"flag","true");

     cJSON_AddItemToObject(subdir,"dira",dir3=cJSON_CreateObject());

     cJSON_AddStringToObject(dir3,"name","uploads");

     cJSON_AddStringToObject(dir3,"path","/uploads/");

     cJSON_AddStringToObject(dir3,"flag","true");

        

     out=cJSON_Print(root);

     cJSON_Delete(root);

     return out;

}

 

 

char * create3()

{

     cJSON *root,*img,*thm;

     char *out;

     int nums[4]={100,200,300,400};

     root=cJSON_CreateObject();

     cJSON_AddItemToObject(root, "Root", img=cJSON_CreateObject());

     cJSON_AddNumberToObject(img,"key",800);

     cJSON_AddNumberToObject(img,"value",600);

     cJSON_AddStringToObject(img,"Title","Sugon");

     cJSON_AddItemToObject(img,"child",thm=cJSON_CreateObject());

     cJSON_AddNumberToObject(thm,"key",125);

     cJSON_AddStringToObject(thm,"value","100");

         cJSON_AddStringToObject(thm,"Url","www.sugon.com");

     cJSON_AddItemToObject(img,"nums", cJSON_CreateIntArray(nums,4));

     out=cJSON_Print(root);

     cJSON_Delete(root);

     return out;

}

 

char * create4()

{

     cJSON *root,*dir1,*dir2;

     char *out;

     const char *ro = "Root";

     root=cJSON_CreateObject();

     cJSON_AddItemToObject(root,ro,dir1=cJSON_CreateArray());

     cJSON_AddNumberToObject(dir1,"key",800);

     cJSON_AddNumberToObject(dir1,"value",600);

     cJSON_AddStringToObject(dir1,"Title","key and value");

     cJSON_AddItemToObject(root,ro,dir2=cJSON_CreateArray());

     cJSON_AddNumberToObject(dir2,"value",125);

     cJSON_AddStringToObject(dir2,"key","100");

     cJSON_AddStringToObject(dir2,"Title","value and key");

     out=cJSON_Print(root);

     cJSON_Delete(root);

     return out;

}

 

void parse1(char *out)

{

     cJSON * root,*arrayItem,*item,*name,*path,*flag;  

     int i = 0,size = 0;

     char *pr = NULL,*na = NULL,*pa = NULL,*fl = NULL;

 

     //将字符串解析成json结构体

     root = cJSON_Parse(out);

     //根据结构体获取数组大小

     size = cJSON_GetArraySize(root);

     //printf("%d\n",size);

     //遍历数组

     for(i=0;i

     {

         //获取第i个数组项

         arrayItem = cJSON_GetArrayItem(root,i);

         if(arrayItem)

         {

              //printf("%s\n","start......");

              //讲json结构体转换成字符串

              pr = cJSON_Print(arrayItem);

              item = cJSON_Parse(pr);

              name = cJSON_GetObjectItem(item,"name");

              path = cJSON_GetObjectItem(item,"path");

              flag = cJSON_GetObjectItem(item,"flag");

              na = cJSON_Print(name);

              pa = cJSON_Print(path);

              fl = cJSON_Print(flag);

              //printf("%s\n",pr);

              printf("name:%s\n",na);

              printf("path:%s\n",pa);

              printf("flag:%s\n\n",fl);

         }

     }

}

 

void parse2(char *out)

{

     cJSON * root,*Root,*Child,*arrayItem,*item,*name,*path,*flag;   

     int i = 0,size = 0;

     char *pr = NULL,*na = NULL,*pa = NULL,*fl = NULL;

     root = cJSON_Parse(out);

     if(root)

     {

         Root = cJSON_GetObjectItem(root,"Root");

         if(Root)

         {

              name = cJSON_GetObjectItem(Root,"name");

              path = cJSON_GetObjectItem(Root,"path");

              flag = cJSON_GetObjectItem(Root,"flag");

              na = cJSON_Print(name);

              pa = cJSON_Print(path);

              fl = cJSON_Print(flag);

              printf("Root:\n");

              printf("name:%s\n",na);

              printf("path:%s\n",pa);

              printf("flag:%s\n\n",fl);

         }

         Child = cJSON_GetObjectItem(root,"Child");

         if(Child)

         {

              size = cJSON_GetArraySize(Child);

              //printf("%d\n",size);

              printf("Child:\n");

              for(i=0;i

              {

                   arrayItem = cJSON_GetArrayItem(Child,i);

                   if(arrayItem)

                   {

                       //printf("%s\n","start......");

                       pr = cJSON_Print(arrayItem);

                       item = cJSON_Parse(pr);

                       name = cJSON_GetObjectItem(item,"name");

                       path = cJSON_GetObjectItem(item,"path");

                       flag = cJSON_GetObjectItem(item,"flag");

                       na = cJSON_Print(name);

                       pa = cJSON_Print(path);

                       fl = cJSON_Print(flag);

                       //printf("%s\n",pr);

                       printf("name:%s\n",na);

                       printf("path:%s\n",pa);

                       printf("flag:%s\n\n",fl);

                   }

              }

         }

     }   

}

 

int main()

{

     char *out1 = create1();

     char *out2 = create2();

     char *out3 = create3();

     char *out4 = create4();

     printf("%s\n\n\n",out1);

     parse1(out1);

     printf("%s\n\n\n",out2);

     parse2(out2);

     printf("%s\n\n\n",out3);

     printf("%s\n\n\n",out4);

     return 0;

}

 

运行结果如下图所示:

  cJSON的构造和解析

cJSON的构造和解析

上图为创建json结构体和解析后的结果图(分别为create1,parse1、create2,parse2),后两个(create3、create4)创建了没有进行解析,因为很简单,自己动手试试吧!相信自己,有些事情其实还是会很容易做到的。

 

 

 

 技术在于交流、沟通,转载请注明出处并保持作品的完整性。

    作者:╰☆奋斗ing❤孩子`  原文:http://blog.sina.com.cn/s/blog_a6fb6cc90101ffme.html。

 

 

 

这篇关于【cJson】cJSON的构造和解析(五)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1123472

相关文章

网页解析 lxml 库--实战

lxml库使用流程 lxml 是 Python 的第三方解析库,完全使用 Python 语言编写,它对 XPath表达式提供了良好的支 持,因此能够了高效地解析 HTML/XML 文档。本节讲解如何通过 lxml 库解析 HTML 文档。 pip install lxml lxm| 库提供了一个 etree 模块,该模块专门用来解析 HTML/XML 文档,下面来介绍一下 lxml 库

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

OWASP十大安全漏洞解析

OWASP(开放式Web应用程序安全项目)发布的“十大安全漏洞”列表是Web应用程序安全领域的权威指南,它总结了Web应用程序中最常见、最危险的安全隐患。以下是对OWASP十大安全漏洞的详细解析: 1. 注入漏洞(Injection) 描述:攻击者通过在应用程序的输入数据中插入恶意代码,从而控制应用程序的行为。常见的注入类型包括SQL注入、OS命令注入、LDAP注入等。 影响:可能导致数据泄

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

CSP 2023 提高级第一轮 CSP-S 2023初试题 完善程序第二题解析 未完

一、题目阅读 (最大值之和)给定整数序列 a0,⋯,an−1,求该序列所有非空连续子序列的最大值之和。上述参数满足 1≤n≤105 和 1≤ai≤108。 一个序列的非空连续子序列可以用两个下标 ll 和 rr(其中0≤l≤r<n0≤l≤r<n)表示,对应的序列为 al,al+1,⋯,ar​。两个非空连续子序列不同,当且仅当下标不同。 例如,当原序列为 [1,2,1,2] 时,要计算子序列 [

leetcode105 从前序与中序遍历序列构造二叉树

根据一棵树的前序遍历与中序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: 3/ \9 20/ \15 7   class Solution {public TreeNode buildTree(int[] pr

多线程解析报表

假如有这样一个需求,当我们需要解析一个Excel里多个sheet的数据时,可以考虑使用多线程,每个线程解析一个sheet里的数据,等到所有的sheet都解析完之后,程序需要提示解析完成。 Way1 join import java.time.LocalTime;public class Main {public static void main(String[] args) thro

ZooKeeper 中的 Curator 框架解析

Apache ZooKeeper 是一个为分布式应用提供一致性服务的软件。它提供了诸如配置管理、分布式同步、组服务等功能。在使用 ZooKeeper 时,Curator 是一个非常流行的客户端库,它简化了 ZooKeeper 的使用,提供了高级的抽象和丰富的工具。本文将详细介绍 Curator 框架,包括它的设计哲学、核心组件以及如何使用 Curator 来简化 ZooKeeper 的操作。 1

Unity3D自带Mouse Look鼠标视角代码解析。

Unity3D自带Mouse Look鼠标视角代码解析。 代码块 代码块语法遵循标准markdown代码,例如: using UnityEngine;using System.Collections;/// MouseLook rotates the transform based on the mouse delta./// Minimum and Maximum values can