Json组件使用说明
该组件为Agent Lite提供给开发者的工具组件,如果开发者无法进行Json格式的编码和解码,则可以使用该组件进行编码和解码。主要用于上报数据组装与下发命令解析。
1. Json编码
使用Json组件进行编码的流程。
创建Json编码对象。
1
|
HW_JSONOBJ HW_JsonObjCreate() |
获取Json对象根节点。
1
|
HW_JSON HW_JsonGetJson(HW_JSONOBJ hjson) |
往Json对象中添加键值对:
添加pcVal为字符串的Json键值对。
1
|
HW_INT HW_JsonAddStr(HW_JSON pstJson, HW_CHAR *pcKey, HW_CHAR *pcVal) |
添加uiVal为整数的Json键值对。
1
|
HW_INT HW_JsonAddUint(HW_JSON pstJson, HW_CHAR *pcKey, HW_UINT uiVal) |
添加bVal为bool的Json键值对。
1
|
HW_INT HW_JsonAddBool(HW_JSON pstJson, HW_CHAR *pcKey, HW_BOOL bVal) |
添加值为Json的Json键值对,获取到的为子Json对象。
1
|
HW_JSON HW_JsonAddJson(HW_JSON pstJson, HW_CHAR *pcKey) |
添加值为Json数组Json键值对,获取到的为子Json数组对象。
1
|
HW_JSON_ARRAY HW_JsonAddArray(HW_JSON pstJson, HW_CHAR *pcKey) |
往Json数组中添加键值对:
添加pcVal为字符串的Json键值对。
1
|
HW_INT HW_JsonArrayAddStr(HW_JSON_ARRAY *pstArray, HW_CHAR *pcKey, HW_CHAR *pcVal) |
添加uiVal为整数的Json键值对。
1
|
HW_INT HW_JsonArrayAddUint(HW_JSON_ARRAY *pstArray, HW_CHAR *pcKey, HW_UINT uiVal) |
添加bVal为bool的Json键值对。
1
|
HW_INT HW_JsonArrayAddBool(HW_JSON_ARRAY *pstArray, HW_CHAR *pcKey, HW_BOOL bVal) |
添加pucValue为Json的Json值,获取到的为子Json对象。
1
|
HW_JSON HW_JsonArrayAddJson(HW_JSON_ARRAY pstArray) |
添加pucValue为Json数组Json键值对,获取到的为子Json数组对象。
1
|
HW_JSON_ARRAY *HW_JsonArrayAddArray(HW_JSON_ARRAY *pstArray) *HW_JsonArrayAddArray(HW_JSON_ARRAY *pstArray) |
获取Json字符串 。
1
|
HW_CHAR *HW_JsonEncodeStr(HW_JSONOBJ hJson); |
删除Json对象 。
1
|
HW_VOID HW_JsonObjDelete(HW_JSONOBJ *phJson); |
Json编码示例:
待解析Json格式:
{ "temperature":22, "otherInfo":{ "batteryLevel":"low" } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/*变量定义*/ HW_JSONOBJ jsonObj; HW_JSON rootjson; HW_JSON json; HW_CHAR *pcJsonStr; /*创建Json编码对象*/ hJsonObj = HW_JsonObjCreate(); /*获取跟节点Json对象*/ rootjson = HW_JsonGetJson(hJsonObj); /*往根节点中添加键值对*/ HW_JsonAddUint(rootjson, "temperature", 22); /*从根节点中获取子Json对象*/ json = HW_JsonAddJson(rootjson, "otherInfo"); /*在子Json中添加键值对*/ HW_JsonAddStr(json, " batteryLevel", "low"); /*获取Json字符串*/ pcJsonStr = HW_JsonEncodeStr(hjsonObj); /*删除之前创建的Json编码对象,释放资源*/ HW_JsonObjDelete(&hJsonObj); |
2. Json解码
使用Json组件进行解码的流程。
创建Json解析对象。
1
|
HW_JSONOBJ HW_JsonDecodeCreate(HW_CHAR *pucStr, HW_BOOL bStrCpy) |
获取Json解析对象中的Json数据部分。
1
|
HW_JSON HW_JsonGetJson(HW_JSONOBJ hJson) |
获取Json数据中与pucKey对应的字符串。
1
|
HW_CHAR *HW_JsonGetStr(HW_JSON pstJson, HW_CHAR *pucKey) |
获取Json数据中与pucKey对应的无符号整型。
1
|
HW_UINT HW_JsonGetUint(HW_JSON pstJson, HW_CHAR *pucKey, HW_UINT uiDft) |
获取Json数据中与pucKey对应的Boolean值。
1
|
HW_BOOL HW_JsonGetBool(HW_JSON pstJson, HW_CHAR *pucKey, HW_BOOL bDft) |
获取Json数据中与pucKey对应的数组。
1
|
HW_UJSON_ARRAY HW_JsonGetArray(HW_JSON pstJson, HW_CHAR *pucKey) |
获取Json数组的长度。
1
|
HW_UINT HW_JsonArrayGetCount(HW_UJSON_ARRAY pstArray) |
获取Json数组中序号为uiIndex项的Json数据。
1
|
HW_JSON HW_JsonArrayGetJson(HW_UJSON_ARRAY pstArray, HW_UINT uiIndex) |
获取Json数组中序号为uiIndex项的无符号整型。
1
|
HW_UINT HW_JsonArrayGetUint(HW_UJSON_ARRAY pstArray, HW_UINT uiIndex, HW_UINT uiDft) |
获取Json数组中序号为uiIndex项的Boolean值。
1
|
HW_UINT HW_JsonArrayGetBool(HW_UJSON_ARRAY pstArray, HW_UINT uiIndex, HW_BOOL bDft) |
获取Json数组中序号为uiIndex项的字符串。
1
|
HW_CHAR *HW_JsonArrayGetStr(HW_UJSON_ARRAY pstArray, HW_UINT uiIndex) |
获取Json数组中序号为uiIndex项的子数组。
1
|
HW_UJSON_ARRAY HW_JsonArrayGetArray(HW_UJSON_ARRAY pstArray, HW_UINT uiIndex) |
删除之前创建的Json解析对象。
1
|
HW_VOID HW_JsonObjDelete(HW_JSONOBJ *phJson) |
Json解析示例:
待解析Json格式:
{ "action":"notify", "type":"userstate", "userstateinfo":[ { "num":"11111 ", "state":"idle" }, { "num":"11111", "state":"ringing" } ] }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/*变量定义*/ HW_JSONOBJ jsonObj; HW_JSON json; HW_UJSON_ARRAY jsonArray; HW_cHAR *action; HW_cHAR *type; HW_UiNT count; HW_UiNT index; /*创建Json解析对象*/ jsonObj = HW_JsonDecodeCreate(jsonStr, HW_TRUE); /*获取Json解析对象中的Json数据部分*/ json = HW_JsonGetJson(jsonObj); /*获取Json数据中与"action"对应的字符串*/ action = HW_JsonGetStr(json, "action"); /*获取Json数据中与"type"对应的字符串*/ type = HW_JsonGetStr(json, "type"); /*获取Json数据中与"userstateinfo"对应的Json数组*/ jsonArray = HW_JsonGetArray(json, "userstateinfo"); /*获取数组jsonArray的长度*/ count = HW_JsonArrayGetCount(jsonArray); for (index = 0; index < count; index++) { /*获取数组jsonArray中序号为index项的Json数据*/ HW_JSON jsonItem = HW_JsonArrayGetJson(jsonArray, index); /*获取jsonItem中与" num "对应的字符串*/ HW_cHAR *num = HW_JsonGetStr(jsonItem, "num"); /*获取jsonItem中与" state "对应的字符串*/ HW_cHAR *state = HW_JsonGetStr(jsonItem, "state"); ...... } /*删除之前创建的Json解析对象,释放资源*/ HW_JsonObjDelete(jsonObj); |