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

 
 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值