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


相关推荐

  • c++获取子类窗口句柄位置_C++中各种获取窗口句柄的方法「建议收藏」

    c++获取子类窗口句柄位置_C++中各种获取窗口句柄的方法「建议收藏」AfxGetMainWndAfxGetMainWnd获取自身窗口句柄HWNDhWnd=AfxGetMainWnd()->m_hWnd;GetTopWindow函数功能:该函数检查与特定父窗口相联的子窗口z序(Z序:垂直屏幕的方向,即叠放次序),并返回在z序顶部的子窗口的句柄。函数原型:HWNDGetTopWindow(HWNDhWnd);参数:hWnd:被查序的父窗口的句柄。如果该…

    2022年7月21日
    9
  • matlab 隶属函数,Matlab中已经开发出了( )种隶属函数。

    matlab 隶属函数,Matlab中已经开发出了( )种隶属函数。不属位的下面行为品定于管哪种中产范畴理学 中已 中已包务方 的等销售 或 质装 价格确定渠道功能产品服务 服量 式等 定义以上根据 标市位是定位品定业为中产指企足目 管产品场理学了满 为公平竞好的环境和条争创造良件 经开不正为当竞制止争行 下列行为作品所产生的 发出保护的是能够著作权法受到 的规定依据有关法律 种隶属保护的著后的有 年的作品作权作者期是发表首次 本案定何认依法应如 函数但双合同终未

    2025年10月4日
    3
  • C#时间控件使用

    C#时间控件使用简单的做法是转换成指定精度的String,然后比较字符串。Dated1=datetimepicker.date;Datenow=DateTime.Now;假设精确到分钟:intresult=now.toString(“yyyy-MM-ddhh:mi”).compareTo(d1.toString(“yyyy-MM-ddhh:mi”));假如result=0

    2022年5月18日
    33
  • 初步STL集装箱Vector

    初步STL集装箱Vector

    2022年1月4日
    133
  • JVM参数解析 Xmx、Xms、Xmn、NewRatio、SurvivorRatio、PermSize、PrintGC「建议收藏」

    -verbose:gc-XX:+printGC可以打印GC的简要信息[GC4790K-&gt;374K(15872K),0.0001606secs][GC4790K-&gt;374K(15872K),0.0001474secs][GC4790K-&gt;374K(15872K),0.0001563secs][GC4790K-&gt;374K(15872K),0.0…

    2022年4月16日
    57
  • 单片机0~10v模拟输出_单片机控制三相直流电机

    单片机0~10v模拟输出_单片机控制三相直流电机写在前面承接上文,本次主要设计就是用的是MSP430F249的主控,文末附上下载链接完成效果注意事项三个时钟都要进行设置,确保时钟为8M方可正常使用我就不多说了直接贴代码吧测频串口发送机oled.c////////////////////////////////////////////////////////////////////////////////// //———————————————-

    2022年5月3日
    43

发表回复

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

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