C语言 JSON数据格式解析

C语言JSON数据格式解析一、如何用c语言编写与解析json数据格式,这篇主要是使用一个第三方的json库,本人已经上传至csdn,下载链接在下方。 二、json库代码文件下载地址(json.rar内部只有两个文件json.h与json.c) 1.http://download.csdn.net/download/jxyb2012/10234057

大家好,又见面了,我是你们的朋友全栈君。

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;
}



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/125723.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 自然常数e的意义

    自然常数e的意义出处 http www fengchang cc post 104 这两天看黎曼猜想的新闻刷屏 虽然这个猜想还未得到证明 当今数学文献中已有超过一千条数学命题以黎曼猜想 或其推广形式 的成立为前提 反复见之于是想了解一下 不过了解之前顺便先看下相关的这个自然对数 中学数学都知道 e 是一个常量 无理数 重要性不亚于圆周率 值约等于 2 71828 但是这完全不能算是一个定义 也完全摸不清头脑这

    2025年11月27日
    5
  • Java是什么?主要是干什么的?「建议收藏」

    Java是什么?主要是干什么的?「建议收藏」随着Java技术不断发展,许多人都想问:Java是什么?主要是干什么的呀?现在小朗来为大家解惑。java是一种高级计算机语言,一种可以编写跨平台应用软件、完全面向对象的程序设计语言。那Java主要是干嘛的呀?一、java可以做网站Java主要可以用于编写网站,如今许多商业网站都用Jsp写的,JSP全称JavaServerPages。它是一种动态网站技术性,例如大家了解的163,一些政府门户网站全是选用JSP撰写的。因此学习培训Java的同学们能够找开发网站层面的工作中,并且…

    2022年7月7日
    25
  • u盘越狱卡代码怎么办_使用爱思助手制作越狱U盘教程

    u盘越狱卡代码怎么办_使用爱思助手制作越狱U盘教程U盘越狱iPhone绕ID最新教程及各种坑解决,吐血之作(超详细超简单教程)-balenaEtcher-Checkm8-bootra1n这是安装多个苹果版本及虚拟机版本后成功的教程,由于资源上传到百度云盘下载只有几十KB,所以为了大家能够尽快的体验上苹果系统,文章中涉及的所有工具请大家加QQ群进行交流下载:1064543120一、工具下载准备一台Windows系统电脑准备一个>2G存储U盘下载群文件中balenaEtcher、Checkm8.info_iCloudBypass、bootr

    2025年12月11日
    2
  • Springboot和Spring的区别?看完你就明白了

    Springboot和Spring的区别?看完你就明白了从一道面试题说起面试的时候经常会被问到,spring和springboot的区别。或者SpringMVC和Springboot的区别。其实这样的问法就不是特别合适。因为spring、springboot、springmvc他们三个在spring体系中就不在同一个维度。看一下spring的全部项目spring家族有很多项目,springboot、springframework、springcloud等。我们常用的也就是,springboot、springcloud、springsecu

    2022年6月8日
    41
  • Javascript作用域问题的构造函数的变量

    Javascript作用域问题的构造函数的变量

    2022年1月4日
    49
  • oracle11g安装完成如何打开界面

    oracle11g安装完成如何打开界面找到oracle11g的安装路径下的tnsnames.ora复制该文件到下面对应的文件替换即可(该文件(压缩包名instantclient_11_2可以自己下载,也可以向我要qq1406697403,下载后任意建一个文件夹如本文oracle11,将该压缩包解压后放到其中)然后做替换(看图)准备好plsql打开如下设置如:现在可以登录了!

    2022年7月25日
    75

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号