您好,欢迎来到纷纭教育。
搜索
您的当前位置:首页c语言实现JSON数据打包和解析

c语言实现JSON数据打包和解析

来源:纷纭教育

注:此程序为小白练手,有诸多不足,请谅解。

/*没有使用malloc和free版*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
///1、各类C库函数的自写
///*******************************************
//程序功能: 计算字符串长度;
//@str:字符串地址;
//@return:count长度;
//说明:计数方式实现my_strlen函数:
//*******************************************/
unsigned int my_strlen(const char* str)
{
    unsigned int count = 0;
    while (*str != '\0')
    {
        count++;
        str++;
    }
    return count;
}
///*******************************************
//程序功能: num个字符串拷贝
//@destination:要拷贝到的目标地址
//@source:被拷贝的源字符串地址
//@num:被拷贝的字符个数
//@return:返回destination地址
//说明:不改变源字符串拷贝
//*******************************************/
char* my_strcpy(char* destination, const char* source)
{
    char* temp = destination;
    while (*source != '\0')
    {
        *temp++ = *source++;
    }
    *temp = '\0';
    return destination;
}
char* my_strncpy(char* destination, const char* source, unsigned int num)
{
    if (destination == NULL || source == NULL)
    {
        return destination; // 输入参数为空指针,直接返回destination
    }

    unsigned int len_s = my_strlen(source); // 获取源字符串的长度

    for (unsigned int i = 0; i < num; i++)
    {
        destination[i] = source[i]; // 逐个拷贝字符
        if (i > len_s) // 当源字符串已经拷贝完时,添加新的终止符
        {
            break;
        }
    }

    if (num > len_s)
    {
        for (unsigned int i = len_s; i < num; i++) // 添加剩余的终止符
        {
            destination[i] = '\0';
        }
    }

    return destination;
}
///*******************************************
//程序功能:将源字符串 (source) 拼接到目标字符串 (destination) 的末尾
//@destination:目标字符串的地址
//@source:源字符串的地址
//@return:返回目标字符串的地址
//*******************************************/
char* my_strcat(char* destination, const char* source)
{
    unsigned int len_s = my_strlen(destination);
    int i=0;
    while(*(source+i)!='\0')
    {
        *(destination + len_s+i) = *(source+i);
        i++;
    }
    *(destination + len_s + i + 1) = '\0';
    return destination;
}
///*******************************************
//程序功能: 字符串插入
//@str1:要拷贝到的目标地址
//@str2:被拷贝的源字符串地址
//@return:返回str1地址
//@position:插入的位置
//说明:字符串合并/插入函数,插入为第position个字符的后面
//*******************************************/
char* str_insert(char* str1, const char* str2, int position)
{
    unsigned int str1_len = my_strlen(str1);
    unsigned int str2_len = my_strlen(str2);
    unsigned int move_len = str1_len - position;
    if (position > str1_len) 
    {
        position = str1_len;
    }
    char* insert_adder = str1 + position;
    for (int i = move_len; i >= 0; i--)
    {
        *(insert_adder + str2_len + i) = *(insert_adder + i);
    }
    *(insert_adder + str2_len + move_len ) ='\0';
    while (*str2 != '\0')
    {
        *insert_adder = *str2;
        str2++;
        insert_adder++;
    }
    return str1;
}
///*******************************************
//程序功能: 字符串搜索函数:在源字符串 (source) 中搜索目标字符串 ( check) 
//@source:源字符串的地址
//@ check:目标字符串的地址
//@return:返回目标字符串末位字符的地址+1
//*******************************************/
const char* serch_str(const char* source, const char* check)
{
    unsigned match = 0;//匹配度
    unsigned flag = 0;//匹配成功标识
    unsigned int len_c = my_strlen(check);
    unsigned int len_s = my_strlen(source);
    const char* add;
    int i = 0;
    int j = 0;
    if (source == NULL || check == NULL)
    {
        return NULL; // 输入参数为空指针,返回空指针
    }
    while (flag == 0)
    {
        if (i < len_s)
        {
            if (match == len_c)
            {
                flag = 1;//p匹配成功
            }
            else if ((match != 0) && (*(source + i) != *(check + j)))//当match!=0;且匹配下一位不成功,回退一位
            {
                i--;
                match = 0;
                j = 0;
            }
            else if (*(source + i) == *(check + j))
            {
                match++;
                j++;
            }
        }
        else
            return NULL;//超出匹配区域,没有匹配成功
        i++;
    }
    add = source + i - 1;
    return add;
}
//2、JSON打包
///*******************************************
//程序功能: 字符数据JSON打包
//@json_key:要拷贝到的目标地址
//@json_value:被拷贝的源字符串地址
//@return:返回打包完成数据地址
//说明:json_str格式为"name": "John",还未加{}
//*******************************************/
 char* Json_Add_Str(const char* json_key, const char* json_value)
{
     ;/*不设置成static变量,在本函数结束时,
      str1会被释放,导致在主函数打印该值时出现
        str1 = 烫烫烫烫烫烫烫烫跟 / */
     static char str1[50] = ""; // 初始化为空字符串
     str1[0] = '\0'; // 清空 str1
     str_insert(str1, "\"", 0); // 插入 json_key
     str_insert(str1, json_key, 1); // 插入 json_key
     str_insert(str1, ":", my_strlen(str1)); // 插入冒号
     str_insert(str1, json_value, my_strlen(str1)); // 插入 json_value
     str_insert(str1, "\"", my_strlen(str1)); // 插入 json_key
     return str1;
}
///*******************************************
//程序功能: 实现字符串JSON数据打包功能
//@return:json_str打包好的json数据
//*******************************************/
 char* json_pack()
{
     static char json_str[100] = "";
     static char json1_str[100]="";
    static  char json2_str[100] = "";
    char *json1 = Json_Add_Str("姓名", "张三");
    size_t json1_len = my_strlen(json1);
    for (int i = 0; i<json1_len; i++)
    {
        json1_str[i] = *(json1 + i);
    }
    json1_str[json1_len + 1] = '\0';
    char *json2 = Json_Add_Str("年龄", "28");
    size_t json2_len = my_strlen(json2);
    for (int i = 0; i < json2_len; i++)
    {
        json2_str[i] = *(json2 + i);
    }
    json2_str[json2_len + 1] = '\0';
    size_t json_str_size = json1_len + json2_len + 2;
    printf("json1=%s\n", json1);
    printf("json2=%s\n", json2);
   
    my_strcpy(json_str, "{");
    my_strcpy(json_str+1, json1_str);
    my_strcpy(json_str + json1_len+1, ",");
    my_strcpy(json_str + json1_len + 2, json2_str);
    my_strcpy(json_str + json1_len + 2+json2_len, "}");
    printf("json_str=%s\n", json_str);
    return json_str;
}
 ///3、JSON数据解析
/***************************************
name:       JSON 校验函数
input:      字符串
output:     合法JAON 返回1 不合法JSON 返回0
description:
***************************************/
char json_check(const char* json)
{
    unsigned int len = my_strlen(json);
    if ((json[0] == '{') && (json[len - 1] == '}'))
    {
        printf("格式正确");
        return 0;//格式正确
    }
    else
    { 
        printf("格式错误");
        return 1;
    } 
}
/***************************************
name:       JSON 键值对比函数
input:      JSON 键值 要匹配的字符
output:     如果匹配成功返回1 失败返回0
description:
***************************************/
char json_check_value(char* str1, char* str2)
{
    if (strcmp(str1, str2) == 0)
    {
        return 1; //匹配成功返回1
    }
    else
    {
        return 0;
        //匹配成功返回0
    }

}
/***************************************
name:       JSON 解析函数
input:      解析函数字符串
output:     合法JAON 返回json_value 不合法JSON 返回NULL
description:
json:传入的json字符串
json_value:解析后的数组
***************************************/    
char* json_anysyst(char* json,const char *json_key,char* json_value)
{
    unsigned int json_key_len = my_strlen(json_key);
    unsigned int json_value_len = 0;//
    char* json_key_end;
    char* json_key_start;
   const char* json_value_start;
    char* json_value_end;
    unsigned int len_j = my_strlen(json);
    int i=0;
    json_value_start = serch_str(json, json_key);
    if ((json[0] == '{') && (json[len_j - 1] == '}'))//格式正确
    {
        if (*json_value_start == '"' && *(json_value_start + 1) == ':' && *(json_value_start + 2) == '"')//判断是否为键值
        {
            json_value_start = json_value_start + 3;
            while (*(json_value_start + json_value_len) != '"')
            {
                json_value_len++;
            }
            my_strncpy(json_value, json_value_start, json_value_len);
            return json_value;
        }
        else
        {
            printf("输入键的格式错\r\n");
            return NULL;
        }
    }
    else
    {
        printf("json数据格式错误\r\n");
        return NULL;
    }
}
int main()
{
    char json[] = "{\"name\":\"小明\",\"age\":\"28\"}";
    char str1[50] = "";
    char str2[50] = "123cde";
    json_anysyst(json,"name", str1);
    printf("s=%s\n", str1);
    return 0;
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- fenyunshixun.cn 版权所有 湘ICP备2023022495号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务