zoukankan      html  css  js  c++  java
  • 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);
            }
    
    
    
        }
    }
  • 相关阅读:
    英文哲理短句
    经历的一次诈骗
    英文哲理短句
    反思对待新人的方式
    Java 开源报表制作
    现在开始写字
    关于Visual C++ 6.0的调试技巧和经验总结
    一步一步教你实现CTreeCtrl 自绘
    VC中动态加载ODBC解决方法
    VC++程序编译链接的原理与过程
  • 原文地址:https://www.cnblogs.com/qingergege/p/6776176.html
Copyright © 2011-2022 走看看