El contenido no se encuentra disponible en el idioma seleccionado. Estamos trabajando continuamente para agregar más idiomas. Gracias por su apoyo.

Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

JSON Component Description

Updated on 2022-02-24 GMT+08:00

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);

Utilizamos cookies para mejorar nuestro sitio y tu experiencia. Al continuar navegando en nuestro sitio, tú aceptas nuestra política de cookies. Descubre más

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback