【Linux读取JSON格式文件】

2 篇文章 0 订阅

#include "math.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <cJSON.h>
#include <sys/socket.h>
#include <unistd.h>

/**************************************************************************
* 函数名称: int get_json_int_para( int *pValue_set, cJSON *pObject, const char *pStringNeedToFind )
* 功能描述: 获取json格式int 型参数的子模块函数
* 输入参数: *pValue_set;需要赋值的参数,pObject;上级cJSON指针 ,pStringNeedToFind;需要查询的键值对字符串
* 输出参数:
* 返 回 值: 0;成功获取参数,-1;未获取到参数
* 其它说明:
* 修改日期    版本号	 修改人		  修改内容
* -----------------------------------------------
* 2022/03/08	  V1.0		 ZhaoChengJie		   first commit
**************************************************************************/
static int get_json_int_para( int *pValue_set, cJSON *pObject, const char *pStringNeedToFind )
{

    cJSON * pItem = cJSON_GetObjectItem ( pObject, pStringNeedToFind ) ;
    if ( NULL != pItem )
    {
        cJSON * pItem_value = cJSON_GetObjectItem ( pItem, "value" ) ;
        if ( NULL != pItem_value )
        {
            *pValue_set = pItem_value->valueint ;

#if PRINTF_ALL_PARA
            printf ( "%s=%d\n", pStringNeedToFind, *pValue_set );
#endif
        }
        else
        {
            printf("(get_json_int_para)no %s\n", pStringNeedToFind);
            return -1 ;
        }
    }
    else
    {
        printf("(get_json_int_para)no %s\n", pStringNeedToFind);
        return -1 ;
    }
    return 0;
}


/**************************************************************************
* 函数名称: int get_json_str_para( char *pValue_set, cJSON *pObject, const char *pStringNeedToFind )
* 功能描述: 获取json格式char 型参数的子模块函数
* 输入参数: pValue_set;需要被赋值的参数,pObject;上级cJSON指针 ,pStringNeedToFind;需要查询的字符串
* 输出参数:
* 返 回 值: 0成功获取参数,-1;未获取到参数
* 其它说明:
* 修改日期    版本号	 修改人		  修改内容
* -----------------------------------------------
* 2022/03/08	  V1.0		 ZhaoChengJie		   first commit
**************************************************************************/
static int get_json_str_para( char *pValue_set, cJSON *pObject, const char *pStringNeedToFind )
{
    int iValueLen;

    cJSON * pItem = cJSON_GetObjectItem ( pObject, pStringNeedToFind )  ;
    if ( NULL != pItem )
    {
        cJSON * pItem_value = cJSON_GetObjectItem ( pItem, "value" )	;
        if ( NULL != pItem_value )
        {
            iValueLen = strlen( pItem_value->valuestring );
            strncpy( pValue_set, pItem_value->valuestring, iValueLen ) ;

#if PRINTF_ALL_PARA
            printf ( "%s=%s\n", pStringNeedToFind, pValue_set );
#endif
        }
        else
        {
            printf("(get_json_str_para)no %s\n", pStringNeedToFind);
            return -1 ;
        }
    }
    else
    {
        printf("(get_json_str_para)no %s\n", pStringNeedToFind);
        return -1 ;
    }
    return 0;
}



/**************************************************************************
* 函数名称: int cjson_read_udp_para( char *pMenu, struct UDP_PARA *udp_para )
* 功能描述: 获取json格式的板载WiFi模块相关参数
* 输入参数: pMenu:传入的参数目录, udp_para 传入的Udp参数结构体
* 输出参数:
* 返 回 值: iResult 成功返回发送的参数指令的长度,失败返回-1 ,具体错误查看errno
* 其它说明:
#define WIFI_PARA_MENU	"/etc/udp_para/wifi_cfg_para.json"
#define SYS_CFG_MENU	"/etc/udp_para/system_cfg.json"

* 修改日期    版本号	 修改人		  修改内容
* -----------------------------------------------
* 2022/03/01	  V1.0		 ZhaoChengJie		   first commit
* 2022/03/10	  V2.0		 ZhaoChengJie		   增加文件目录输入接口,输入目录为NULL时,使用默认目录

**************************************************************************/
int cjson_read_udp_para ( char *pMenu, struct UDP_PARA *udp_para )
{
    struct UDP_PARA *pT_udp_para = (struct UDP_PARA *)udp_para;

    FILE 		   *pFp = NULL;
    cJSON		   *pJson;
    char 		   *pOut;
    int iLen;
    int iCnt;
    char* pData;
    int iRetAccess;

    udp_para_init(pT_udp_para);//初始化所有参数
    //iRetAccess =  access ( WIFI_PARA_MENU , F_OK ) ;
    iRetAccess =  access ( pMenu, F_OK ) ;
    printf ( "(cjson_read_udp_para)menu=%s\n",pMenu );
    if ( -1 == iRetAccess )
    {
        printf ( "(cjson_read_udp_para)can not find the menu:%s \n\n", pMenu );
        iRetAccess =  access ( WIFI_PARA_MENU, F_OK ) ;
        if ( -1 == iRetAccess )
        {
            printf ( "(cjson_read_udp_para)can not find the local cjson_read_udp_para menu:%s \n\n", WIFI_PARA_MENU  );
            return -1;
        }
        else
        {
            pFp = fopen ( WIFI_PARA_MENU, "r" ); //as4_comm_result->_file_menu.process_menu
        }
    }
    else
    {
        pFp = fopen ( pMenu, "r" );
    }



    if( NULL == ( pFp ) )
    {
        return -1;
    }
    fseek ( pFp, 0, SEEK_END );
    iLen = ftell( pFp );
    fseek( pFp, 0, SEEK_SET );
    pData = ( char*)malloc( iLen + 1 );
    fread( pData, 1, iLen, pFp ) ;
    fclose( pFp );
    if ( NULL != ( pFp ) )
    {
        //PX("data=%s\n",data );
        pJson=cJSON_Parse ( pData ); //获取整个大的句柄
        if ( ( NULL != pJson ) && ( pJson->type == cJSON_Object ) )
        {
            pOut = cJSON_Print ( pJson ); //这个是可以输出的。为获取的整个json的值
#if PRINTF_ALL_PARA
            printf ( "(cjson_read_udp_para)json pack into cjson success!!! \n" ); 一串数字也会被解析为json格式
#endif
        }
        else
        {
            printf ( "(cjson_read_udp_para)json pack into cjson error \n" );
            return -2;
        }
            cJSON * item_remote_ip = cJSON_GetObjectItem ( item_socket_set, "remote_ip" )  ;
            if ( NULL != item_remote_ip )
            {
                strncpy( pT_udp_para->socket_set.cRemoteIP, item_remote_ip->valuestring, strlen( item_remote_ip->valuestring ) ) ;
#if 1
                printf ( "RemoteIP=%s\n", pT_udp_para->socket_set.cRemoteIP );
#endif
            }
            else
            {
                printf("(cjson_read_udp_para)no cRemoteIP\n");
            }
        }
        else
        {
            printf("(cjson_read_udp_para)no local_port\n");
        }

        cJSON * pUDP_para = cJSON_GetObjectItem ( pJson, "udp_para" )  ;
        if ( NULL != pUDP_para )
        {
            get_json_int_para( (int *)&pT_udp_para->HT_TxStream.iValue_set, pUDP_para, "nvram_set_HT_TxStream" );
            get_json_int_para( (int *)&pT_udp_para->HT_RxStream.iValue_set, pUDP_para, "nvram_set_HT_RxStream" );
            get_json_int_para( (int *)&pT_udp_para->WlanSiteSurvey.iValue_set, pUDP_para, "nvram_set_WlanSiteSurvey" );
            get_json_str_para( pT_udp_para->GatewayWanSet_tmp.cValue_set_ip, pUDP_para, "nvram_set_GatewayWanSet_tmp_ip" );

        }
        else
        {
            printf("(cjson_read_udp_para)no cur file \n");
        }
    }

    cJSON_Delete ( pJson );
    free ( pData );
    return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值