C语言 JSON数据格式解析
C语言 JSON数据格式解析
一、如何用c语言编写与解析json数据格式,这篇主要是使用一个第三方的json库,本人已经上传至csdn,下载链接在下方。
二、json库代码文件下载地址(json.rar内部只有两个文件json.h与json.c)
1.http://download.csdn.net/download/jxyb2012/10234057
三、json数据结构(下面程序代码演示如何使用json第三方库编码与解析这么一个json数据)
{
"uid":100,
"username":"admin",
"weaps":[1,2,3,4,5],
"member":
{
"uid":10010,
"username":"user"
}
}
程序代码
//main.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "./../3rd/json/json.h" int main(int argc, char** argv) { //创建root节点 json_t* root = json_new_object(); //添加uid与username到root json_insert_pair_into_object(root, "uid", json_new_number("100")); json_insert_pair_into_object(root, "username", json_new_string("admin")); //添加weaps到root json_t* json_array_weaps = json_new_array(); char num_text[16]; for (int i = 1; i <= 6; i++) { json_insert_child(json_array_weaps, json_new_number(itoa(i, num_text, 10))); } json_insert_pair_into_object(root, "weaps", json_array_weaps); //添加member到root//添加uid与username到member json_t* json_object_member = json_new_object(); json_insert_pair_into_object(json_object_member, "uid", json_new_number("10010")); json_insert_pair_into_object(json_object_member, "username", json_new_string("user")); json_insert_pair_into_object(root, "member", json_object_member); //json text char* json_text; //把json tree保存到字符串 json_tree_to_string(root, &json_text); printf("json_text:\n%s\n", json_text); free(json_text); //保存文件 FILE* fp = NULL; fp = fopen("test.json", "w+"); if (fp == NULL) { goto failed; } json_stream_output(fp, root); fflush(fp); //释放资源 fclose(fp); json_free_value(&root); / //读取文件 json_t* document = NULL; fp = fopen("test.json", "r"); if (fp == NULL) { goto failed; } //解析文件到json document json_stream_parse(fp, &document); if (document == NULL) { goto failed; } //查找int类型 uid; json_t* key = json_find_first_label(document, "uid"); if (key) { json_t* value = key->child; if (value->type == JSON_NUMBER) { printf("value is number:%d\n", atoi(value->text)); } } //查找string类型 username; key = json_find_first_label(document, "username"); if (key) { json_t* value = key->child; if (value->type == JSON_STRING) { printf("value is string:%s\n", value->text); } } //查找数组类型 weaps; key = json_find_first_label(document, "weaps"); if (key) { json_t* value = key->child; if (value->type == JSON_ARRAY) { json_t* child_value = value->child; static unsigned int count = 0; while (child_value) { count++; if (child_value->type == JSON_NUMBER) { printf("array value[%d] : %i\n", count, atoi(child_value->text)); } child_value = child_value->next; } } } key = json_find_first_label(document, "member"); if (key) { json_t* value = key->child; if (value->type == JSON_OBJECT) { json_t* child_key = json_find_first_label(value, "uid"); if (child_key) { json_t* child_value = child_key->child; if (child_value->type == JSON_NUMBER) { printf("value is number:%d\n", atoi(child_value->text)); } } } } //释放资源 fclose(fp); json_free_value(&document); failed: system("pause"); return 0; }
C语言 JSON数据格式解析相关推荐
- JSON 数据格式解析(转)
JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互.本文将快速讲解 JSON 格式,并通过代码示例演示如 ...
- JSON数据格式解析库(cJSON、Jansson)的使用在STM32上移植和使用
json | json-c使用入门 这篇讲的也不错,抽空看下(网络传输json数据) https://www.bilibili.com/video/av669454528?p=3&spm_id ...
- go语言json字符串解析为结构体数组,结构体指针的数组
废话不多说直接上效果图 完整代码如下 package main import ("encoding/json""fmt" ) // 结构体定义 type rob ...
- 推荐一个Chrome插件--JSON数据格式解析编辑插件--JSON-handle
对JSON格式的内容进行浏览和编辑,以树形图样式展现JSON文档,并可实时编辑插件: https://chrome.google.com/webstore/detail/json-handle/iah ...
- android 解析json数据格式
前面我有用到android发送json数据:这里我想总结一下我用到的解析json数据格式的方式 json数据格式解析我自己分为两种: 一种是普通的,一种是带有数组形式的: 普通形式的: 服务器端返回的 ...
- 传递json_开发技巧分享—JSON 数据格式及函数讲解
FileMaker 16 发布之后新增了 6 个 JSON 系列函数,这对开发 FileMaker 解决方案来说简直如虎添翼. JSON 数据格式解析 对于做网站开发的前后端工程师来说,JSON 数据 ...
- 将html代码确析成json数据格式,JSON字符串解析成JSON数据格式
在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 使用eval函数来解析,并且使用jquery的ea ...
- c语言组json包,json格式解析和libjson的用法介绍(关于cjson的使用方法)
在阅读本文之前,请先阅读下<Rss Reader实例开发之系统设计>一文. Rss Reader实例开发中,进行网络数据交换时主要使用到了两种数据格式:JSON与XML.本文主要介绍JSO ...
- c语言json结构体_C语言解析JSON源码
2020-01-09 关键字:cJSON.linux JSON解析 JSON 是一种在互联网领域内很常用的轻量级数据交换协议. 它与 XML 的地位差不多,但就笔者而言,笔者更喜欢 JSON 的风格, ...
最新文章
- mac 安装Pillow
- 基于OpenGL编写一个简易的2D渲染框架-03 渲染基本几何图形
- 安徽省计算机一级文化基础,计算机一级文化基础选择题
- FLP不可能性(FLP impossibility)
- android原生接入rn,Android原生项目集成RN页面
- html网页如何获取后台数据库的数据(html + ajax + php + mysql)
- C# 反射 设置字段值无效的解决办法
- matlab 转 python_985工科硕士自学转程序员经验
- 前端技术周刊 2019-02-11 Serverless
- matlab 二元函数的画法
- 使用dropwizard(5)--加入swagger
- 代码 点胶gcode_3D打印机启停代码Gcode
- 一文带你了解Unity Shader-小飞侠轻功(径向模糊)
- 死循环之----恐怖游轮
- 【语音智能管家】之语音唤醒(附演示视频)
- cad图纸解析java_Java中的AutoCAD库可读取.dwg文件?
- ubuntu 22.04右上角找不到wifi图标,有线网络也失效
- 寒武纪如何查看mlu的运行状态
- 存在ai * aj = ak
- 大数据元数据管理系统有哪些功能
热门文章
- 关于IDDR与ODDR以及IBUFDS和OBUFDS的使用
- 编程小石头点餐小程序_点餐小程序,点餐系统,管理后台批量导入excel菜品数据...
- Xilinx原语ODDR的使用
- 题解报告——天使玩偶
- 编写文档的5W2H原则
- OpenLayers学习笔记6——使用jQuery UI实现查询并标注(功能实现篇)
- Mac如何卸载软件 最简单的mac卸载软件方法
- 2021-2027全球与中国家畜口蹄疫 (FMD) 疫苗市场现状及未来发展趋势
- Glyce: Glyph-vectors for Chinese Character Representations
- methods中方法互相调用