JSON Component Description
The JSON component is provided by the AgentLite to developers for encoding and decoding data in JSON format. It is used to assemble reported data and deliver command parsing results.
1. JSON Encoding
The JSON encoding process is as follows:
Create a JSON coding object.
1 |
HW_JSONOBJ HW_JsonObjCreate()
|
Obtain the root node of the JSON object.
1 |
HW_JSON HW_JsonGetJson(HW_JSONOBJ hjson)
|
Add key-value pairs to the JSON object.
Add the key-value pair in which the pcVal is a character string.
1 |
HW_INT HW_JsonAddStr(HW_JSON pstJson, HW_CHAR *pcKey, HW_CHAR *pcVal)
|
Add the key-value pair in which the uiVal is an integer.
1 |
HW_INT HW_JsonAddUint(HW_JSON pstJson, HW_CHAR *pcKey, HW_UINT uiVal)
|
Add the key-value pair in which the bVal is a Boolean value.
1 |
HW_INT HW_JsonAddBool(HW_JSON pstJson, HW_CHAR *pcKey, HW_BOOL bVal)
|
Add the key-value pair whose value is a JSON object to obtain a sub-JSON object.
1 |
HW_JSON HW_JsonAddJson(HW_JSON pstJson, HW_CHAR *pcKey)
|
Add the key-value pair whose value is a JSON array to obtain a sub-JSON array object.
1 |
HW_JSON_ARRAY HW_JsonAddArray(HW_JSON pstJson, HW_CHAR *pcKey)
|
Add key-value pairs to the JSON array.
Add the key-value pair in which the pcVal is a character string.
1 |
HW_INT HW_JsonArrayAddStr(HW_JSON_ARRAY *pstArray, HW_CHAR *pcKey, HW_CHAR *pcVal)
|
Add the key-value pair in which the uiVal is an integer.
1 |
HW_INT HW_JsonArrayAddUint(HW_JSON_ARRAY *pstArray, HW_CHAR *pcKey, HW_UINT uiVal)
|
Add the key-value pair in which the bVal is a Boolean value.
1 |
HW_INT HW_JsonArrayAddBool(HW_JSON_ARRAY *pstArray, HW_CHAR *pcKey, HW_BOOL bVal)
|
Add the key-value pair in which the pucValue is a JSON object to obtain a sub-JSON object.
1 |
HW_JSON HW_JsonArrayAddJson(HW_JSON_ARRAY pstArray)
|
Add the key-value pair in which the pucValue is a JSON array to obtain a sub-JSON array object.
1 |
HW_JSON_ARRAY *HW_JsonArrayAddArray(HW_JSON_ARRAY *pstArray) *HW_JsonArrayAddArray(HW_JSON_ARRAY *pstArray)
|
Obtain JSON character strings.
1 |
HW_CHAR *HW_JsonEncodeStr(HW_JSONOBJ hJson);
|
Delete the JSON object.
1 |
HW_VOID HW_JsonObjDelete(HW_JSONOBJ *phJson);
|
Encoding Example:
A JSON message to be parsed is as follows:
{ "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 |
/*Define variables.*/
HW_JSONOBJ jsonObj;
HW_JSON rootjson;
HW_JSON json;
HW_CHAR *pcJsonStr;
/*Create a JSON encoding object.*/
hJsonObj = HW_JsonObjCreate();
/*Obtain the root node of the JSON object. */
rootjson = HW_JsonGetJson(hJsonObj);
/*Add a key-value pair to the root node.*/
HW_JsonAddUint(rootjson, "temperature", 22);
/*Obtain the sub-JSON object from the root node.*/
json = HW_JsonAddJson(rootjson, "otherInfo");
/*Add a key-value pair to the sub-JSON object.*/
HW_JsonAddStr(json, " batteryLevel", "low");
/*Obtain JSON character strings. */
pcJsonStr = HW_JsonEncodeStr(hjsonObj);
/*Delete the JSON encoding object that was previously created, to release resources.*/
HW_JsonObjDelete(&hJsonObj);
|
2. JSON Decoding
The JSON decoding process is as follows:
Create a JSON parsing object.
1 |
HW_JSONOBJ HW_JsonDecodeCreate(HW_CHAR *pucStr, HW_BOOL bStrCpy)
|
Obtain JSON data from the JSON parsing object.
1 |
HW_JSON HW_JsonGetJson(HW_JSONOBJ hJson)
|
Obtain the character string mapping to the pucKey from the JSON data.
1 |
HW_CHAR *HW_JsonGetStr(HW_JSON pstJson, HW_CHAR *pucKey)
|
Obtain the unsigned integer mapping to the pucKey from the JSON data.
1 |
HW_UINT HW_JsonGetUint(HW_JSON pstJson, HW_CHAR *pucKey, HW_UINT uiDft)
|
Obtain the Boolean value mapping to the pucKey from the JSON data.
1 |
HW_BOOL HW_JsonGetBool(HW_JSON pstJson, HW_CHAR *pucKey, HW_BOOL bDft)
|
Obtain the array mapping to the pucKey from the JSON data.
1 |
HW_UJSON_ARRAY HW_JsonGetArray(HW_JSON pstJson, HW_CHAR *pucKey)
|
Obtain the length of the JSON array.
1 |
HW_UINT HW_JsonArrayGetCount(HW_UJSON_ARRAY pstArray)
|
Obtain JSON data mapping to the uiIndex from the JSON array.
1 |
HW_JSON HW_JsonArrayGetJson(HW_UJSON_ARRAY pstArray, HW_UINT uiIndex)
|
Obtain the unsigned integer mapping to the uiIndex from the JSON array.
1 |
HW_UINT HW_JsonArrayGetUint(HW_UJSON_ARRAY pstArray, HW_UINT uiIndex, HW_UINT uiDft)
|
Obtain the Boolean value mapping to the uiIndex from the JSON array.
1 |
HW_UINT HW_JsonArrayGetBool(HW_UJSON_ARRAY pstArray, HW_UINT uiIndex, HW_BOOL bDft)
|
Obtain the character string mapping to the uiIndex from the JSON array.
1 |
HW_CHAR *HW_JsonArrayGetStr(HW_UJSON_ARRAY pstArray, HW_UINT uiIndex)
|
Obtain the sub-array mapping to the uiIndex from the JSON array.
1 |
HW_UJSON_ARRAY HW_JsonArrayGetArray(HW_UJSON_ARRAY pstArray, HW_UINT uiIndex)
|
Delete the JSON parsing object that was previously created.
1 |
HW_VOID HW_JsonObjDelete(HW_JSONOBJ *phJson)
|
Parsing Example:
A JSON message to be parsed is as follows:
{ "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 |
/*Define variables.*/
HW_JSONOBJ jsonObj;
HW_JSON json;
HW_UJSON_ARRAY jsonArray;
HW_cHAR *action;
HW_cHAR *type;
HW_UiNT count;
HW_UiNT index;
/*Create a JSON parsing object.*/
jsonObj = HW_JsonDecodeCreate(jsonStr, HW_TRUE);
/*Obtain JSON data from the JSON parsing object.*/
json = HW_JsonGetJson(jsonObj);
/*Obtain the character string mapping to the action from the JSON data.*/
action = HW_JsonGetStr(json, "action");
/*Obtain the character string mapping to the type from the JSON data.*/
type = HW_JsonGetStr(json, "type");
/*Obtain the JSON array mapping to the userstateinfo from the JSON data.*/
jsonArray = HW_JsonGetArray(json, "userstateinfo");
/*Obtain the length of the jsonArray.*/
count = HW_JsonArrayGetCount(jsonArray);
for (index = 0; index < count; index++)
{
/*Obtain JSON data mapping to the index from the jsonArray.*/
HW_JSON jsonItem = HW_JsonArrayGetJson(jsonArray, index);
/*Obtain the character string mapping to the num from the jsonItem.*/
HW_cHAR *num = HW_JsonGetStr(jsonItem, "num");
/*Obtain the character string mapping to the state from the jsonItem.*/
HW_cHAR *state = HW_JsonGetStr(jsonItem, "state");
......
}
/*Delete the JSON parsing object that was previously created, to release resources.*/
HW_JsonObjDelete(jsonObj);
|
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot