首页 > 其他分享 >cjson 用法

cjson 用法

时间:2023-12-21 14:35:50浏览次数:32  
标签:JsonMain TempPtrArray cjson 用法 cJSON TempPtr include

 

1、修改字典的值

cJSON_SetValuestring(objectItem,value)
//先获取object
TempPtr = cJSON_GetObjectItem(TempPtr,"nm");
//修改该object 的值
cJSON_SetValuestring(TempPtr,"guxiang de hai");

2、删除数组里面的值

cJSON_DeleteItemFromArray(Array,index);
// Array 要删除的数组,index 要删除的索引
TempPtrArray = cJSON_GetObjectItem(JsonMain,"items");
cJSON_DeleteItemFromArray(TempPtrArray,0);

 3、转为字符串

cJSON_PrintUnformatted(root);

const char *strData = result->data.c_str();
cJSON *root = cJSON_Parse(strData);
//修改完root的值后,重新赋值
char* newStr =  cJSON_PrintUnformatted(root);
result->data = newStr;
cJSON_free(newStr);
cJSON_Delete(root);

代码:

数据:

{
    "tt1":"1234",
    "tt2":"4321",
    "items": [
        {"nm":"DConHis_recordTime"},
        {"nm":"DConHis_GetFlag","va":"1"}
    ]
}

代码:

#include<stdio.h>
#include <stdlib.h>
#include<unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "lib/cjson/include/cjson/cJSON.h"
#include <iostream>
static char* cfgPath = "./tt.txt";

int main(int argc, char** argv)
{
char Buf[4096] = {0};
    int bfd = open(cfgPath,O_RDWR);
    read(bfd,Buf,4095);
    fprintf(stdout,"\t\t\t\t read json Buf:[%s]",Buf);
    close(bfd);

    //解析我们读到的Json字符串
    cJSON* JsonMain = cJSON_Parse(Buf);

    //删除tt1的节点
    cJSON_DeleteItemFromObject(JsonMain,"tt1");

    //定位到tt2的节点,并修改其值
    cJSON* TempPtr = cJSON_GetObjectItem(JsonMain,"tt2");
    cJSON* TempPtrArray = NULL;
    cJSON_SetValuestring(TempPtr,"Now End!");

    //定位数组,因为他属于itmes节点,所以找到items节点,它的值就是数组了
    TempPtrArray = cJSON_GetObjectItem(JsonMain,"items");
    //定位第二个数组,因为索引从0开始计算,所以参数为1
    TempPtr = cJSON_GetArrayItem(TempPtrArray,1);
    //定位到nm节点,并修改nm节点的数值
    TempPtr = cJSON_GetObjectItem(TempPtr,"nm");
    //更改json 字典的value;
    cJSON_SetValuestring(TempPtr,"guxiang de hai");
    //删除Array 的第一条数据
    cJSON_DeleteItemFromArray(TempPtrArray,0);

    fprintf(stdout,"\nAfter Delete TempPtrmArray index 0 :\n");
    char* str1= cJSON_Print(TempPtrArray);
    fprintf(stdout,"result TempPtrArray :\n%s\n",str1);

    fprintf(stdout,"reslut JsonMain\n");

    str1= cJSON_Print(JsonMain);
    fprintf(stdout,"\t\t\t\t result json :\n%s\n",str1);
    //释放资源
    cJSON_Delete(JsonMain);
    cJSON_free(str1);
    return 0;
}

编译:

g++ delete_array.cpp lib/cjson/include/cjson/cJSON.h -L./lib/cjson/lib64/ -lcjson  -Wl,-rpath=./lib/cjson/lib64

 

标签:JsonMain,TempPtrArray,cjson,用法,cJSON,TempPtr,include
From: https://www.cnblogs.com/lovychen/p/17918954.html

相关文章

  • json.NewDecode用法
    Go使用RFC3339进行编码,如果控制生成的json,只需将2022-04-03T00:00:00.000更改为2022-04-03T00:00:00.000Z。例如,这是有效的。typeContractstruct{ContractId*int`json:"contract_id"`CompanyId*int`json:"company_id"`Dat......
  • MySQL——子查询用法
    1、子查询概述子查询指一个查询语句嵌套在另一个查询语句内部的查询,内部的查询是外部查询的条件,这个特性从MySQL4.1开始引入。SQL中子查询的使用大大增强了SELECT查询的能力,因为很多时候查询需要从结果集中获取数据,或者需要从同一个表中先计算得出一个数据结果,然后与这个数据结果(可......
  • odoo rpc用法
     js /**@odoo-module**/import{registry}from"@web/core/registry";import{Layout}from"@web/search/layout";import{getDefaultConfig}from"@web/views/view";import{Component,onWillStart,useSubEnv}from&q......
  • printf, fprintf, sprintf, vsprintf, snprintf, vsnprintf之间的用法和区别
    1、printf是打印格式化字符串,返回值是成功打印的字符数;intprintf(constchar*format,...);  2、fprintf是将格式化数据写入到指定文件流中;intfprintf(FILE*stream,constchar*format,...);  3、sprintf是将格式化的数据写入到指定字符串;intsprintf(char*str,......
  • ElasticSearch中查询语句用法(match、match_phrase、multi_match、query_string)
    1、match略1.1不同字段权重如果需要为不同字段设置不同权重,可以考虑使用bool查询的should子句来组合多个match查询,并为每个match查询设置不同的权重。{"query":{"bool":{"should":[{"match":{"product_name":......
  • drf( Restful规范——序列化与反序列化——DjangoRESTframewordk用法——原生与drf实
    #Restful规范(重要-概念)```python#API接口---》后端要提供api接口---》2000年RoyFielding的博士论文中###restful是什么?REST全称是RepresentationalStateTransfer,表征性状态转移WebAPI接口的设计风格,尤其适用于前后端分离的应用模式中#有哪些规范10条 1数据的安全......
  • js中?.、??、??=的用法及使用场景
    js中?.、??、??=的用法及使用场景小熊爱敲代码征途慢慢,唯有奋斗​关注她 你经常看TA的内容  上面这个错误,相信前端开发工程师应该经常遇到吧,要么是自己考虑不全造成的,要么是后端开发人员丢失数据或者传输错误数据类型造成的。因此对数据访问......
  • CURL用法
    curl是一个常用的命令行工具,用于获取或发送数据,支持多种协议,如HTTP、HTTPS、FTP等。以下是使用curl的例子:获取网页内容:curlhttps://www.example.com这个命令会将https://www.example.com的HTML源码打印到控制台。发送POST请求:curl-d"param1=value1&param2=value2"-XP......
  • 自定义元素宽高比例(aspect-ratio)与 @supports兼容支持和图片裁剪(object-fit)的用法
    1、aspect-ratio宽高比例属性aspect-ratio:1/1;aspect-ratio:16/9;aspect-ratio:0.5;//等同于1/2如下效果将为每个box子元素设置aspect-ratio:3/2,如下图所示: 2、object-fit图片裁剪object-fit:contain;保持宽高比,缩放保持图片完整性。object-fit:cover......
  • border-image用法总结
    border-image支持渐变,可实现虚线边框,斑马纹边框border-image支持在外部显示图像,不占空间,不影响布局,且不受overflow:hidden限制border-image,box-shadow,outline均支持内填充,外填充,可以实现背景,边框,外延border-image内填充border-image:linear-gradient(rgba(0,0,0,.05),......