最近在處理vod網路應用的時候,碰到乙個按id排序顯示的需求,跟大家分享下,此**片段通過vs2008除錯完成。
1、json str排序前後列印。
2、基本的處理思路如下。
(1)構建乙個json input str;
(2)申請結構體資源,解析並儲存;
(3)針對此結構體資料做排序;
(4)用排序後的結構體資料填充,並構造新的json output str;
(5)釋放結構體資源。
3、附上**片段。
// cjson_learning.cpp : 定義控制台應用程式的入口點。
//#include "stdafx.h"
#include "cjson.h"
#define cjson_log printf
//#define cjson_debug printf
#define cjson_debug(...) do{}while(0)
typedef struct
struct_channel_data;
static struct_channel_data *channel_data = ;
static int g_channel_num;//全域性變數,儲存channel的總個數
void release_struct_channel_data(void)
if(channel_data[i].name != null)}}
g_channel_num = 0;
cjson_debug("\n[%s]after release:g_channel_num:%u\n",__function__,g_channel_num);
}// 使用開源的cjson api建立乙個新的json input string
static char* create_new_json_input_str(int channel_num)
, ,,,
, };
int i = 0;
new_json_root = cjson_createarray();
if(new_json_root == null)
for(i = 0; i < channel_num; i++)
cjson_additemtoarray(new_json_root, new_object);
cjson_addstringtoobject(new_object, "id", data[i][0]);
cjson_addstringtoobject(new_object, "name",data[i][1]);
}
json_string = cjson_print(new_json_root);
cjson_log("%s\n",json_string);
if(json_string == null)
end:
cjson_delete(new_json_root);
return json_string;
}// 使用開源的cjson api建立乙個新的json output string
static char* create_new_json_output_str(int channel_num)
for(i = 0; i < channel_num; i++)
cjson_additemtoarray(new_json_root, new_object);
cjson_addstringtoobject(new_object, "id", channel_data[i].id);
cjson_addstringtoobject(new_object, "name", channel_data[i].name);
}
json_string = cjson_print(new_json_root);
cjson_log("%s\n",json_string);
if(json_string == null)
end:
cjson_delete(new_json_root);
return json_string;
}int _tmain(int argc, _tchar* ar**)
channel_num = cjson_getarraysize(root);
cjson_debug("channel_num:%d\n",channel_num);
// 3.用結構體儲存json input str解析後的各個字段
channel_data = (struct_channel_data*)malloc(channel_num*sizeof(struct_channel_data));
memset(channel_data,0,channel_num*sizeof(struct_channel_data));
g_channel_num = channel_num;
cjson_debug("[%s]g_channel_num:%u\n",__function__,g_channel_num);
cjson *p_object = null;
cjson *p_id = null;
cjson *p_name = null;
cjson *p_number = null;
cjson *p_season = null;
cjson *p_link = null;
for(i = 0;i < channel_num;i++)
p_name = cjson_getobjectitem(p_object, "name");
if(p_name)
}
}
for(i = 0;i < channel_num;i++)
// 4.比較"id" value的大小,按從小到大排序(按物件整體排序)
int j = 0;
char temp[20];
char temp1[128];
for(i=0;i < channel_num;i++)}}
cjson_log("\n\n\n\n\n>>>>>>>>>>>>>>>>>>after sort>>>>>>>>>>>>>>>>>>>>>>>>\n");
for(i = 0;i < channel_num;i++)
// 5.構造乙個新的json output str
char *new_json_str = create_new_json_output_str(channel_num);
// 6.釋放申請的全域性結構體空間
release_struct_channel_data();
system("pause");
return 0;
}
json問題小結
json 鍵值對增加 刪除 obj.key value obj.key obj key eval obj.key delete obj.key vue中新增和刪除屬性 this.set object,key,value this.delete object,key 觸發檢視更新 遍歷鍵值 for v...
json 日期問題
2008 6 19 13 57 39 net.sf.json.jsonobject morphpropertyvalue 警告 can t transform property birthday from j a.lang.string into j a.util.date.will registe...
json 時區問題
使用postman 測試時,返回的資料少了8小時。在除錯中,測試資料是正確的,包括介面返回也是正確的。很明顯在轉json時出了問題。先說一下,不要使用jackson來作為json元件。先說一下,不要使用jackson來作為json元件。先說一下,不要使用jackson來作為json元件。文末,我會說...