CJSON解析json字符串示例

cJSON 作为 Json 格式的解析库,其主要功能就是构建和解析 Json 格式

CJSON解析json字符串 {"action":"started","code":"0","data":"","desc":"success","sid":"a8"}

复制代码
#include <stdio.h>  
#include <stdlib.h>  
#include "cJSON.h"  
  
int main() {  
    char *json_string = "{\"action\":\"started\",\"code\":\"0\",\"data\":\"\",\"desc\":\"success\",\"sid\":\"a8\"}";  
      
    // 解析JSON字符串  
    cJSON *root = cJSON_Parse(json_string);  
    if (root == NULL) {  
        printf("Error before: [%s]\n", cJSON_GetErrorPtr());  
        return 1;  
    }  
      
    // 获取各个字段的值  
    cJSON *action = cJSON_GetObjectItem(root, "action");  
    cJSON *code = cJSON_GetObjectItem(root, "code");  
    cJSON *data = cJSON_GetObjectItem(root, "data");  
    cJSON *desc = cJSON_GetObjectItem(root, "desc");  
    cJSON *sid = cJSON_GetObjectItem(root, "sid");  
      
    // 输出各个字段的值  
    printf("action: %s\n", action->valuestring);  
    printf("code: %s\n", code->valuestring);  
    printf("data: %s\n", data->valuestring);  
    printf("desc: %s\n", desc->valuestring);  
    printf("sid: %s\n", sid->valuestring);  
      
    // 释放内存  
    cJSON_Delete(root);  
      
    return 0;  
}
相关推荐
我是Superman丶5 分钟前
Element UI 表格某行突出悬浮效果
前端·javascript·vue.js
恋猫de小郭6 分钟前
你的代理归我了:AI 大模型恶意中间人攻击,钱包都被转走了
前端·人工智能·ai编程
Dream of maid19 分钟前
Linux(下)
linux·运维·服务器
齐鲁大虾22 分钟前
统信系统UOS常用命令集
linux·运维·服务器
xiaokuangren_33 分钟前
前端css颜色
前端·css
weixin_4460235643 分钟前
C语言:面向过程、应用底层开发、跨平台的通用程序设计语言
c语言·跨平台·数据类型·底层开发·面向过程
hoiii1871 小时前
C# 基于 LumiSoft 实现 SIP 客户端方案
前端·c#
anOnion1 小时前
构建无障碍组件之Meter Pattern
前端·html·交互设计
FreakStudio1 小时前
MicroPython LVGL基础知识和概念:底层渲染与性能优化
python·单片机·嵌入式·电子diy
小码哥_常2 小时前
Spring Boot配置diff:解锁配置管理新姿势
前端