栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【Linux读取JSON格式文件】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Linux读取JSON格式文件】

#include "math.h"
#include 
#include 
#include 
#include 
#include 
#include 


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=%dn", pStringNeedToFind, *pValue_set );
#endif
        }
        else
        {
            printf("(get_json_int_para)no %sn", pStringNeedToFind);
            return -1 ;
        }
    }
    else
    {
        printf("(get_json_int_para)no %sn", pStringNeedToFind);
        return -1 ;
    }
    return 0;
}



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=%sn", pStringNeedToFind, pValue_set );
#endif
        }
        else
        {
            printf("(get_json_str_para)no %sn", pStringNeedToFind);
            return -1 ;
        }
    }
    else
    {
        printf("(get_json_str_para)no %sn", pStringNeedToFind);
        return -1 ;
    }
    return 0;
}




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=%sn",pMenu );
    if ( -1 == iRetAccess )
    {
        printf ( "(cjson_read_udp_para)can not find the menu:%s nn", 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 nn", 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=%sn",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=%sn", pT_udp_para->socket_set.cRemoteIP );
#endif
            }
            else
            {
                printf("(cjson_read_udp_para)no cRemoteIPn");
            }
        }
        else
        {
            printf("(cjson_read_udp_para)no local_portn");
        }

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/768443.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号