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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SAE J1939 – 简短介绍[通俗易懂]

    SAE J1939 – 简短介绍[通俗易懂]SAEJ1939–简短介绍在商用车辆领域,标准化的,串行的协议用于单个电子控制单元(ECU)和传动系统组件之间的通讯已有一段时间。通过使用标准化的串行协议,可具有以下优势:组件制造商只需要采用一个协议;这主要是商用车辆才会涉及的问题,因为生产量低。商用车辆制造商可依靠不同供应商的组件。可确保组件之间的互操作性,来自不同制造商的组件不用调整就可一同工作。由国际汽车工程师协会…

    2022年5月1日
    57
  • nginx的负载均衡算法有哪些_负载均衡策略

    nginx的负载均衡算法有哪些_负载均衡策略1、轮询(默认)特点:每个请求按时间顺序逐一分配到不同的后端服务器处理。适用业务场景:后端服务器硬件性能配置完全一致,业务无特殊要求时适用。upstreamtomcats{ server192.168.1.173:8080; server192.168.1.175:8080;}2、加权轮询特点:指定轮询几率,weight值(权重)和访问比例成正比,用户请求按权重比例分配。适用业务场景:后端服务器硬件性能处理能力不平均的情形。upstreamtomcats{ server

    2022年8月31日
    4
  • 在Linux系统中安装pycharm详解

    在Linux系统中安装pycharm详解1 在官网下载 pycharm 安装包 1 1 进入官网 DownloadPyCh PythonIDEfor 选择 Professional 专业版 直接下载 自己可以选择 pycharm 的不同版本 1 2 进入跳转页面后 出现提示框 点 savefile 1 3 下载完成后 打开安装包下载的位置 2 安装 pycharm2 1 解压文件 2 打开终端 进入 pych

    2025年6月20日
    4
  • 一个服务器上运行多个tomcat,显示总启动某一个特定tomcat

    一个服务器上运行多个tomcat,显示总启动某一个特定tomcat

    2021年7月17日
    72
  • Ubuntu 更新源方法[通俗易懂]

    Ubuntu 更新源方法[通俗易懂]阿里源:https://opsx.alibaba.com/mirrordebhttp://mirrors.aliyun.com/ubuntu/bionicmainrestricteduniversemultiversedeb-srchttp://mirrors.aliyun.com/ubuntu/bionicmainrestricteduniversemultiver…

    2022年5月14日
    44
  • 帝国cms“建立目录不成功,请检查目录权限”的解决方法

    帝国cms“建立目录不成功,请检查目录权限”的解决方法

    2021年11月17日
    38

发表回复

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

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