大家好,又见面了,我是你们的朋友全栈君。
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.cn/125723.html原文链接:https://javaforall.cn