cjson 构造

    cJSON *pJsonTop =  = cJSON_CreateObject();;/*json configuration, using to read and write config file */
    if(pAlertConfStatic->ValidBoxRatio != 0.f)
    {
        cJSON *pJsonSubcp = cJSON_CreateNumber(pJsonSub->valuedouble);
        cJSON_AddItemToObject( pJsonTopcp, "ValidBoxRatio", pJsonSubcp);
    }
    if(pAlertConfStatic->areaOverlapRatio != 0.f)
    {
        cJSON *pJsonSubcp = cJSON_CreateNumber(pJsonSub->valuedouble);
        cJSON_AddItemToObject( pJsonTopcp, "areaOverlapRatio", pJsonSubcp);
    }
    if(pAlertConfStatic->StopDuration != 0)
    {
        cJSON *pJsonSubcp = cJSON_CreateNumber(pAlertConfStatic->StopDuration);
        cJSON_AddItemToObject( pJsonTopcp, "StopDuration", pJsonSubcp);
    }
    if(pAlertConfStatic->TripWireNum)
    {
        cJSON *pJsonSubcp =  cJSON_CreateObject();
        for(int i = 0; i < pAlertConfStatic->TripWireNum; i++)
        {
            cJSON *pJsonItem =  cJSON_CreateObject();
            cJSON *pJsonAttrWarning =  cJSON_CreateObject();

            if((pAlertConfStatic->iva_TripWire_conf[i].direct)
            {
                cJSON *pJsonDirect =  cJSON_CreateNumber(pAlertConfStatic->iva_TripWire_conf[i].direct);
                cJSON_AddItemToObject( pJsonAttrWarning, "direct", pJsonDirect);
            } 

            cJSON *pJsonPointOne =  cJSON_CreateObject();
            cJSON *pJsonPointTwo =  cJSON_CreateObject();
           
            cJSON *pJsonPointOneX = cJSON_CreateNumber(pAlertConfStatic->iva_TripWire_conf[i].warning[0].x);
            cJSON *pJsonPointOneY = cJSON_CreateNumber(pAlertConfStatic->iva_TripWire_conf[i].warning[0].y);
            cJSON_AddItemToObject( pJsonPointOne, "x", pJsonPointOneX);
            cJSON_AddItemToObject( pJsonPointOne, "y", pJsonPointOneY);

            cJSON *pJsonPointTwoX = cJSON_CreateNumber(pAlertConfStatic->iva_TripWire_conf[i].warning[1].x);
            cJSON *pJsonPointTwoY = cJSON_CreateNumber(pAlertConfStatic->iva_TripWire_conf[i].warning[1].y);
            cJSON_AddItemToObject( pJsonPointTwo, "x", pJsonPointTwoX);
            cJSON_AddItemToObject( pJsonPointTwo, "y", pJsonPointTwoY);

            cJSON_AddItemToObject( pJsonAttrWarning, "p0", pJsonPointOne);
            cJSON_AddItemToObject( pJsonAttrWarning, "p1", pJsonPointTwo);
            cJSON_AddItemToObject( pJsonItem, "warning", pJsonAtrrWarning);
            cJSON_AddItemToObject( pJsonSubcp, pAlertConfStatic->iva_TripWire_conf[i].name, pJsonItem);
        }
        cJSON_AddItemToObject( pJsonTopcp, "TripWire", pJsonSubcp);
    }

    if(pAlertConfStatic->climbWallNum)
    {
        cJSON *pJsonSubcp =  cJSON_CreateObject();
        for(int i = 0; i < pAlertConfStatic->climbWallNum; i++)
        {
            cJSON *pJsonItem =  cJSON_CreateObject();
            cJSON *pJsonLineOne=  cJSON_CreateObject();
            cJSON *pJsonLinegOnePointOne =  cJSON_CreateObject();
            cJSON *pJsonLinegOnePointTwo =  cJSON_CreateObject();
            cJSON *pJsonLineTwo =  cJSON_CreateObject();
            cJSON *pJsonLinegTwoPointOne =  cJSON_CreateObject();
            cJSON *pJsonLinegTwoPointTwo =  cJSON_CreateObject();

            cJSON *pJsonLinegOnePointOneX = cJSON_CreateNumber(pAlertConfStatic->iva_climb_conf[i].tripWire[0].warning[0].x);
            cJSON *pJsonLinegOnePointOneY = cJSON_CreateNumber(pAlertConfStatic->iva_climb_conf[i].tripWire[0].warning[0].y);
            cJSON_AddItemToObject( pJsonLineOnePointOne, "x", pJsonLinegOnePointOneX);
            cJSON_AddItemToObject( pJsonLineOnePointOne, "y", pJsonLinegOnePointOneY);
            cJSON_AddItemToObject( pJsonLineOne, "p0", pJsonLineOnePointOne);

            cJSON *pJsonLinegOnePointTwoX = cJSON_CreateNumber(pAlertConfStatic->iva_climb_conf[i].tripWire[0].warning[1].x);
            cJSON *pJsonLinegOnePointTwoY = cJSON_CreateNumber(pAlertConfStatic->iva_climb_conf[i].tripWire[0].warning[1].y);
            cJSON_AddItemToObject( pJsonLineOnePointTwo, "x", pJsonLinegOnePointTwoX);
            cJSON_AddItemToObject( pJsonLineOnePointTwo, "y", pJsonLinegOnePointTwoY);
            cJSON_AddItemToObject( pJsonLineOne, "p1", pJsonLineOnePointTwo);

            cJSON *pJsonLinegTwoPointOneX = cJSON_CreateNumber(pAlertConfStatic->iva_climb_conf[i].tripWire[1].warning[0].x);
            cJSON *pJsonLinegTwoPointOneY = cJSON_CreateNumber(pAlertConfStatic->iva_climb_conf[i].tripWire[1].warning[0].y);
            cJSON_AddItemToObject( pJsonLineTwoPointOne, "x", pJsonLinegTwoPointOneX);
            cJSON_AddItemToObject( pJsonLineTwoPointOne, "y", pJsonLinegTwoPointOneY);
            cJSON_AddItemToObject( pJsonLineTwo, "p0", pJsonLineTwoPointOne);

            cJSON *pJsonLinegTwoPointTwoX = cJSON_CreateNumber(pAlertConfStatic->iva_climb_conf[i].tripWire[1].warning[1].x);
            cJSON *pJsonLinegTwoPointTwoY = cJSON_CreateNumber(pAlertConfStatic->iva_climb_conf[i].tripWire[1].warning[1].y);
            cJSON_AddItemToObject( pJsonLineTwoPointTwo, "x", pJsonLinegTwoPointTwoX);
            cJSON_AddItemToObject( pJsonLineTwoPointTwo, "y", pJsonLinegTwoPointTwoY);
            cJSON_AddItemToObject( pJsonLineTwo, "p1", pJsonLineTwoPointTwo);

            cJSON_AddItemToObject( pJsonItem, "warningOne", pJsonLineTwo);
            cJSON_AddItemToObject( pJsonItem, "warningTwo", pJsonLineTwo);

            cJSON_AddItemToObject( pJsonSubcp, pAlertConfStatic->iva_climb_conf[i].name, pJsonItem);
        }

        cJSON_AddItemToObject( pJsonTopcp, "climbWall", pJsonSubcp);
    }

    if(pAlertConfStatic->areaInvasionNum)
    {
        cJSON *pJsonSubcp =  cJSON_CreateObject();
        for(int i = 0; i < pAlertConfStatic->areaInvasionNum; i++)
        {
            cJSON *pJsonItem =  cJSON_CreateObject();

            cJSON *pJsonSidesNum =  cJSON_CreateNumber(pAlertConfStatic->areaInvasion[i].sidesNum);
            cJSON_AddItemToObject( pJsonItem, "sidesNum", pJsonSidesNum);

            for(int j = 0; j < pAlertConfStatic->areaInvasion[i].sidesNum; j++)
            {
                cJSON *pJsonPoint =  cJSON_CreateObject();
                cJSON *pJsonPointX = cJSON_CreateNumber(pAlertConfStatic->areaInvasion[i].sides[j].x);
                cJSON *pJsonPointY = cJSON_CreateNumber(pAlertConfStatic->areaInvasion[i].sides[j].y);
                cJSON_AddItemToObject( pJsonPoint, "x", pJsonPointX);
                cJSON_AddItemToObject( pJsonPoint, "y", pJsonPointY);
                
                char pointName[16];
                memset(pointName, 0, sizeof(16));
                sprintf(pointName, "p%d", i);
                cJSON_AddItemToObject( pJsonItem, pointName, pJsonPoint);
            }

            cJSON_AddItemToObject( pJsonSubcp, pAlertConfStatic->areaInvasion[i].name, pJsonItem);
        }

        cJSON_AddItemToObject( pJsonTopcp, "areaInvasion", pJsonSubcp);
    }

    if(pAlertConfStatic->areaLeaveNum)
    {
        cJSON *pJsonSubcp =  cJSON_CreateObject();
        for(int i = 0; i < pAlertConfStatic->areaLeaveNum; i++)
        {
            cJSON *pJsonItem =  cJSON_CreateObject();

            cJSON *pJsonSidesNum =  cJSON_CreateNumber(pAlertConfStatic->areaLeave[i].sidesNum);
            cJSON_AddItemToObject( pJsonItem, "sidesNum", pJsonSidesNum);

            for(int j = 0; j < pAlertConfStatic->areaLeave[i].sidesNum; j++)
            {
                cJSON *pJsonPoint =  cJSON_CreateObject();
                cJSON *pJsonPointX = cJSON_CreateNumber(pAlertConfStatic->areaLeave[i].sides[j].x);
                cJSON *pJsonPointY = cJSON_CreateNumber(pAlertConfStatic->areaLeave[i].sides[j].y);
                cJSON_AddItemToObject( pJsonPoint, "x", pJsonPointX);
                cJSON_AddItemToObject( pJsonPoint, "y", pJsonPointY);
                
                char pointName[16];
                memset(pointName, 0, sizeof(16));
                sprintf(pointName, "p%d", i);
                cJSON_AddItemToObject( pJsonItem, pointName, pJsonPoint);
            }

            cJSON_AddItemToObject( pJsonSubcp, pAlertConfStatic->areaLeave[i].name, pJsonItem);
        }

        cJSON_AddItemToObject( pJsonTopcp, "areaLeave", pJsonSubcp);
    }

    ret = APP_OK;
    response.ret_code = ret;
    ml_syscp_submit_rescmd(0, out, (char *)&response, sizeof(response));

    APP_DBGPOS_ERR(APP_SYSMGR, "out stream_process_get_video  result[%d] \n",ret);
   //create all json objects from pAlertConf, ???

    if(pJsonTopcp == NULL)
    {
        return 0;
    }
    char *pBuf = malloc(MAX_ALERT_CONF_BUF_LEN);
    if(pBuf == NULL)
    {
        assert(0);
    }
    memset(pBuf, 0, MAX_ALERT_CONF_BUF_LEN);
    snprintf(pBuf, MAX_ALERT_CONF_BUF_LEN - 1, "%s", cJSON_Print(pJsonTopcp));
    cJSON_Delete(pJsonTopcp);

    *ppbuf = pBuf;

    return 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
cJSON是一个用于在C语言中操作JSON数据的开发库。您可以通过在Linux系统上编译和安装cJSON库来使用它。以下是使用cJSON库创建和操作JSON数据的示例代码: ```c #include <stdio.h> #include "cJSON.h" int main() { cJSON *root = cJSON_CreateObject(); // 创建根节点 cJSON *item = cJSON_CreateObject(); // 创建semantic节点 cJSON *next = cJSON_CreateObject(); // 创建slots节点 cJSON_AddItemToObject(root, "rc", cJSON_CreateNumber(0)); // 在根节点下添加rc节点 cJSON_AddItemToObject(root, "operation", cJSON_CreateString("CALL")); // 在根节点下添加operation节点 cJSON_AddItemToObject(root, "service", cJSON_CreateString("telephone")); // 在根节点下添加service节点 cJSON_AddItemToObject(root, "text", cJSON_CreateString("打电话给张三")); // 在根节点下添加text节点 cJSON_AddItemToObject(root, "semantic", item); // 在根节点下添加semantic节点 cJSON_AddItemToObject(item, "slots", next); // 在semantic节点下添加slots节点 cJSON_AddItemToObject(next, "name", cJSON_CreateString("张三")); // 在slots节点下添加name节点 printf("%s\n", cJSON_Print(root)); // 打印整个JSON字符串 return 0; } ``` 这段代码将创建一个包含您提供的样例JSON数据的JSON对象,并打印出整个JSON字符串。您可以根据您的需求修改和扩展这段代码来操作JSON数据。 请注意,您需要在编译和链接过程中将cJSON库链接到您的项目中。您可以通过克隆cJSON源码并将其添加到您的项目中来完成这一步骤。有关如何在Linux上使用cJSON库的更多信息,请参考cJSON库的文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值