Linux下利用json-c從一個json數組中提取每一個元素中的部分字段組成一個新json數組


先把代碼貼上來,有時間整理一下

 

 

首先說一下要實現的功能:

假定現在有一個json格式的字符串,而且他是一個josn中的數組,比如:

[
        {
            "id": "NEW2017042605516200",
            "id_type": 0,
            "time": "1493270962"
        },
        {
            "id": "20170426A08ZPL00",
            "id_type": 0,
            "time": "1493270924"
        },
        {
            "id": "NEW2017042701070500",
            "id_type": 0,
            "time": "1493270901"
        }
]

假如說我只關心id和time字段,我希望提取這兩個字段,也就是說從這個數組的每一個元素中提取id和time字段,最后組成一個新的數組,新的數組效果如下:

 

[
        {
            "id": "NEW2017042605516200",
            "time": "1493270962"
        },
        {
            "id": "20170426A08ZPL00",
            "time": "1493270924"
        },
        {
            "id": "NEW2017042701070500",
            "time": "1493270901"
        }
 ]

 

關鍵函數的代碼如下

函數1:將一個字符串轉換成json-c中的json_object格式

int string_to_json(char *string, unsigned long len, json_object **json)
{
    json_tokener *tok = json_tokener_new();//創建一個json_tokener對象,以便進行轉換,記得要釋放
    enum json_tokener_error jerr;
    do
    {
        *json = json_tokener_parse_ex(tok, string, len);
    }while((jerr = json_tokener_get_error(tok)) == json_tokener_continue);
    if(jerr != json_tokener_success  || tok->char_offset < (int)len)
    {
        json_tokener_free(tok);
        return -1;
    }
    if(json_object_get_type(*json) == json_type_object || json_object_get_type(*json) == json_type_array)
    {
        json_tokener_free(tok);
        return 0;
    }
    json_tokener_free(tok);
    return -1;
}

函數2:

在一個json_object中遞歸查找指定的key的value,(注意:沒有處理json數組的情況)

提取的value保存在了參數value中,

注意我沒有處理json_object為數組的情況,此外如果其中嵌套了數組也不能找出來

//第歸查找單個字段
void get_json_value(json_object *json, const char *key ,char value[])
{
    if(json_object_get_type(json) == json_type_object ) 
    {
        json_object_object_foreach(json,json_key,json_value)
        {
            if(strcmp(json_key, key) == 0)
            {
                strcpy(value,json_object_get_string(json_value));
                return;
            }
            get_json_value(json_value,key,value);
        }
    }

}

函數3:

實現想要的功能

//從一個json的數組中抽取每個元素的部分字段,組成一個新的json數組
//其中new_json為原始數組,key為待提取的字段的名字,index為對應的新的字段的名字,num表示提取的字段的個數,
//value存儲最后抽取出來的json數組
void parse_json_arr(json_object *new_json,char keys[][MAX_KEY_LEN],char indexs[][MAX_KEY_LEN],int num,char value[])
{

    //必須保證new_json是一個數組
    if(json_object_get_type(new_json) == json_type_array)
    {
        int ele_num = json_object_array_length(new_json);
        if(ele_num > ARR_MAX_NUM)
            ele_num = ARR_MAX_NUM;    //只取60個元素
        
        //最終組成的json數組
        json_object *array_json = json_object_new_array();//記得要釋放
        int i = 0;
        //遍歷整個數組
        for(;i < ele_num;i++)
        {
            //每一個元素
            json_object *ele_json = json_object_array_get_idx(new_json,i);
            //提取部分字段后
            json_object *selected_json = json_object_new_object();//應該也不用釋放

            int tag = 0;
            int j = 0;
            //重新組裝但個元素(提取其中的幾個字段)
            for(;j < num; j++)
            {
                char selected_value[MAX_SING_VALUE_LEN];
                memset(selected_value,0,MAX_SING_VALUE_LEN);
                get_json_value(ele_json, keys[j], selected_value);
                json_object *json_part = json_object_new_string(selected_value);//這個應該是不用釋放
                json_object_object_add(selected_json,indexs[j],json_part);

                tag = 1;

            }
            if(tag == 1)
            {
                json_object_array_add(array_json, selected_json);
            }

        }
        //將json轉換成字符串
        strncpy(value,json_object_get_string(array_json),MAX_VALUE_LEN);
        //釋放
        if(json_object_get_type(array_json) != json_type_null)
        {
            json_object_put(array_json);
        }



    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM