The upu_struct.h encapsulates the the contents of the pmtinfo structure, as shown in the following sections:
Use the Jansson library to convert the data content contained in the incoming struct pmtinfo into a JSON string to return
The code is as follows:
#include <stdio.h> #include <string.h> #include "jansson.h" #include "upu_struct.h" #include "upu_proto_ Parse.h "///Pmtinfo"/////Nnum The number of incoming mtinfo structures///Plen-C structure converted to JSON structure string length//implementation function:// Converts data content contained in a struct pmtinfo into a JSON string return char* struct_to_json_n (mtinfo *pmtinfo, int nnum, int *plen) {json_t *object,*a Rray;int I,size;array=json_array (); for (i=0;i<nnum;++i) {object=json_object ();//struct PMTINFO contains data content const char * Pmoid=mt_get_moid (&pmtinfo[i]); const char* pe164=mt_get_e164 (&pmtinfo[i]); Const char* pprototype=mt_get_ Prototype (&pmtinfo[i]); const char* Pmttype=mt_get_mttype (&pmtinfo[i]); const char* Pmtstate=mt_get_mtstate ( &pmtinfo[i]); const char* Pmtip=mt_get_mtip (&pmtinfo[i]); const char* Pnuip=mt_get_nuip (&pmtinfo[i]); Const char* Puserdomain=mt_get_userdomain (&pmtinfo[i]); const char* Pdevid=mt_get_devid (&pmtinfo[i]);//Structure The data content contained in Pmtinfo is converted to JSON format json_t *pjsonmoid=json_string (pmoid); json_t *pjsone164=json_string (pE164); json_t *pjsonprototype=json_string (Pprototype); json_t *pjsonmttype=json_string (PMttype); JSON _t *pjsonmtstate=json_string (pmtstate); json_t *pjsonmtip=json_string (Pmtip); json_t *pjsonnuip=json_string (PNuip); json_t *pjsonuserdomain=json_string (puserdomain); json_t *pjsondevid=json_string (Pdevid);// The contents of the JSON structure are integrated into objectjson_object_set_new (object, "Moid", pjsonmoid); Json_object_set_new (object, "E164", pJsonE164); Json_object_set_new (Object, "Prototype", Pjsonprototype); Json_object_set_new (object, "Mttype", pjsonmttype); json_ Object_set_new (Object, "Mtstate", pjsonmtstate); Json_object_set_new (object, "Mtip", Pjsonmtip); json_object_set_new (Object, "Nuip", Pjsonnuip), Json_object_set_new (object, "UserDomain", Pjsonuserdomain); Json_object_set_new (object, "Devid", pjsondevid); int Size1=json_object_size (object);p rintf ("[%d]:size1=%d\n", i,size1);// Converts an array of this struct into a JSON-formatted string char *curresult=null; Curresult=json_dumps (object, Json_preserve_order);p rintf ("curresult=%s\n", curresult);//Add this string to JSOn the struct array json_array_insert_new (array,i,object);} Size=json_array_size (Array);p rintf ("size=%d\n", size), char *result=json_dumps (array,json_preserve_order); return result; }int Main () {int i = 0; int nlen = 0; int nnum = 5;char *pjsontostring=null; Mtinfo *pmtinfoarray = NULL; Pmtinfoarray = (Mtinfo *) malloc (nnum * sizeof (mtinfo)); memset (Pmtinfoarray, 0, Nnum * sizeof (mtinfo)); Through the function mt_set_e164 set E164 number 0512111885621 ~ 0512111885625 to Structure Pmtinfoarray[n] mt_set_e164 (&pmtinfoarray[0], "051 2111885621 "); mt_set_e164 (&pmtinfoarray[1], "0512111885622"); mt_set_e164 (&pmtinfoarray[2], "0512111885623"); mt_set_e164 (&pmtinfoarray[3], "0512111885624"); mt_set_e164 (&pmtinfoarray[4], "0512111885625"); Pjsontostring=struct_to_json_n (Pmtinfoarray, Nnum, &nlen); printf ("pjsontostring=%s\n", pjsontostring); return 0;}
The compilation results are as follows:
Converts the data content contained in the incoming struct pmtinfo into a JSON string to return