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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JSON连续解析指的是在C语言中,将多个连续的JSON数据进行解析。粘包问题是指在网络传输中,多个数据包粘在一起,造成解析困难。 对于C语言JSON连续解析,可以使用第三方库如cJSON实现。cJSON是一个用于解析和创建JSON数据C语言库,它提供了一些函数用于解析和生成JSON数据。 当存在多个连续的JSON数据时,我们可以将它们存储在一个缓冲区中,然后逐个解析每个JSON数据,直到全部解析完成。解析时需要注意数据包的边界,防止解析过程中发生越界访问。 在解析过程中,可以通过循环来逐个解析JSON数据。首先,我们需要找到第一个JSON数据的起始位置,并将其传递给cJSON解析函数。解析成功后,我们可以获取JSON的各个字段值,并对其进行处理。 然后,我们需要找到第二个JSON数据的起始位置,并再次调用cJSON解析函数进行解析。以此类推,直到所有连续的JSON数据都被解析完毕。 在解析过程中,要注意处理粘包问题。如果发现解析到的数据长度不够一个完整的JSON数据,可以等待接收到足够的数据后再进行解析。如果出现粘包,可以根据JSON数据的特定格式进行处理,将多个连续的JSON数据分割开来。 综上所述,C语言JSON连续解析主要涉及使用第三方库cJSON解析多个连续的JSON数据,同时需要注意处理粘包问题。通过逐个解析JSON数据并处理粘包情况,我们可以实现对连续JSON数据解析和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值