/* Parse text to JSON, then render back to text, and print! */ void parseJSON(char *text) { char *out;cJSON *json; json=cJSON_Parse(text); if (!json) {printf("Error before: [%s]\n",cJSON_GetErrorPtr());} else { cJSON *responseData = cJSON_GetObjectItem(json,"responseData"); cJSON *results = cJSON_GetObjectItem(responseData,"results"); cJSON *firstRes = results->child; char* str_value = cJSON_GetObjectItem(firstRes,"unescapedUrl")->valuestring; char* str_value1 = cJSON_GetObjectItem(firstRes,"titleNoFormatting")->valuestring; out=cJSON_Print(firstRes); fprintf(stdout,"First URL: %s\n", str_value); fprintf(stdout,"First Title: %s\n", str_value1); // out=cJSON_Print(json); cJSON_Delete(json); // printf("%s\n",out); free(out); } }
void parse(scientist_t ** sc) { FILE * file = fopen("Scientist.json", "r"); char text[10000]; char line[100]; while(fgets(line, 100, file) != NULL) { strcat(text, line); } fclose(file); cJSON * jList = cJSON_Parse(text); if (!jList) { printf("Error before: [%s]\n", cJSON_GetErrorPtr()); return; } for (int i = 0; i < cJSON_GetArraySize(jList); i++) { cJSON * jItem = cJSON_GetArrayItem(jList, i); char * name = cJSON_GetObjectItem(jItem, "name")->valuestring; char * surname = cJSON_GetObjectItem(jItem, "surname")->valuestring; char * date = cJSON_GetObjectItem(jItem, "date")->valuestring; int salary = cJSON_GetObjectItem(jItem, "salary")->valueint; double age = cJSON_GetObjectItem(jItem, "age")->valuedouble; Scientist_set(sc[i], name, surname, age, date, salary); } cJSON_Delete(jList); }
int read_json(int argc, char **argv, cJSON **result) { char *file_name; char *file; cJSON *json; int error; file_name = (argc >= 2) ? argv[1] : "netsocket.json"; log_info("Opening file %s...", file_name); error = file_to_string(file_name, &file); if (error) return error; json = cJSON_Parse(file); if (!json) { log_err("JSON syntax error."); log_err("The JSON parser got confused around about here:"); log_err("%s", cJSON_GetErrorPtr()); free(file); return 1; } free(file); *result = json; return 0; }
char *doit(char *text,const char *item_str) { char *out=NULL;cJSON *json,*item_json; json=cJSON_Parse(text); if (!json) {printf(NET_LIB_TAG"Error it before: [%s]\n",cJSON_GetErrorPtr());} else { item_json = cJSON_GetObjectItem(json, item_str); if (item_json) { cJSON *data; data=cJSON_GetObjectItem(item_json,item_str); if(data) { int nLen = strlen(data->valuestring); out=(char *)malloc(nLen+1); memset(out,'\0',nLen+1); memcpy(out,data->valuestring,nLen); } } cJSON_Delete(json); } return out; }
void doit(char *text) { cJSON *json; json = cJSON_Parse(text); if (!json) { printf("Error before: [%s]\n", cJSON_GetErrorPtr()); } else { int code = cJSON_GetObjectItem(json, "code")->valueint; // printf("Code is %d\n", code); char *text, *url; switch(code) { case 100000: text = cJSON_GetObjectItem(json, "text")->valuestring; printf("@%s\n", text); break; case 200000: text = cJSON_GetObjectItem(json, "text")->valuestring; url = cJSON_GetObjectItem(json, "url")->valuestring; printf("@%s\n%s\n", text, url); break; case 40001: case 40003: fprintf(stderr, "Keys error, changing to old version robot\n"); exit(3); case 40004: fprintf(stderr, "Visit Limit\n"); exit(4); } } }
void parse(char * text, admin ** p) { cJSON * jList = cJSON_Parse(text); if (!jList) { printf("Error before[%s]\n", cJSON_GetErrorPtr()); return 1; } else{ int count = cJSON_GetArraySize(jList); for (int i = 0; i < count; i++) { cJSON * jItem = cJSON_GetArrayItem(jList, i); char * name = cJSON_GetObjectItem(jItem, "name")->valuestring; char * surname = cJSON_GetObjectItem(jItem, "surname")->valuestring; char * birthdate = cJSON_GetObjectItem(jItem, "birthdate")->valuestring; cJSON * jGroup = cJSON_GetObjectItem(jItem, "company"); char * companyName = cJSON_GetObjectItem(jGroup, "Network")->valuestring; char * language = cJSON_GetObjectItem(jGroup, "language")->valuestring; int release = cJSON_GetObjectItem(jGroup, "release")->valueint; printf("Admin%i :\nName: %s\tSurnm:%s\t birthdate %s\t Working :\n Company :%s Language:%s Release %i \n",i + 1, name, surname, birthdate, companyName, language, release); // set_admin(p[i,name,surname,birthdate,company,hours); } } cJSON_Delete(jList); }
int http_get_warningduration(int *value) { int rt; char result_str[MAX_RESULT] = {0}; char response_json_str[MAX_RESULT] = {0}; const char url_str[]= "http://127.0.0.1/cgi-bin/rest/network/GatewaySettingOperation.cgi?operatortype=0¶m1=0"; cJSON *response_json, *json_temp; rt = http_curl_get(url_str, result_str); if(rt <0) { GDGL_DEBUG("invoke api failed\n"); return -1; } get_json_str(response_json_str, result_str); response_json = cJSON_Parse(response_json_str); if (!response_json) { GDGL_DEBUG("json parse Error before: (%s)\n", cJSON_GetErrorPtr()); return -2; } json_temp = cJSON_GetObjectItem(response_json, "warningduration"); if(!json_temp) { GDGL_DEBUG("cJSON_GetObjectItem failed\n"); cJSON_Delete(response_json); return -2; } *value = json_temp->valueint; cJSON_Delete(response_json); return 0; }
// Obstacle Avoidance Task Json Object Parsing Functions // Entrance Gate: 1, 2, 3 // Exit Gates: 4, 5, 6 (x, y, z) int obstacleParse_Entrance(char *str){ if(str == NULL){ printf("Error: Passed NULL as Input Argument\n"); return -1; } else{ cJSON *tmp = cJSON_Parse(str); char numConv[1]; if(!tmp){ printf("Error before: [%s]\n",cJSON_GetErrorPtr()); return -1; } else { char *gateInfo = cJSON_GetObjectItem(tmp,"gateCode")->valuestring; numConv[0]=gateInfo[1]; switch(atoi(numConv)){ case 1: { return 1; break; } case 2: { return 2; break; } case 3: { return 3; break; } default: { return 0; break; } } } } }
int obstacleParse_Exit(char *str){ if(str == NULL){ printf("Error: Passed NULL as Input Argument\n"); return -1; } else{ cJSON *tmp = cJSON_Parse(str); if(!tmp){ printf("Error before: [%s]\n",cJSON_GetErrorPtr()); return -1; } else{ char *gateInfo = cJSON_GetObjectItem(tmp,"gateCode")->valuestring; if(gateInfo[3]=='X'||gateInfo[4]=='X'){ return 4; } else if(gateInfo[3]=='Y'||gateInfo[4]=='Y'){ return 5; } else if(gateInfo[3]=='Z'||gateInfo[4]=='Z'){ return 6; } else{ return 0; } } } }
/* Parse text to JSON, then render back to text, and print! */ void doit(char *text) { char *out = NULL; cJSON *json = NULL; printf("parse\n"); json = cJSON_Parse(text); if (!json) { printf("Error before: [%s]\n", cJSON_GetErrorPtr()); } else { //JsonRecursiveReadExample(json); printf("printf json\n"); out = cJSON_Print(json); cJSON_Delete(json); if(out == NULL){ printf("malloc error\n"); free(out); } printf("%s\n", out); free(out); } }
// Docking Task Json Object Parsing Function // Cross: 1, Triangle: 2, Circle: 3 int dockingParse(char *str){ if(str == NULL){ printf("Error: Passed NULL as Input Argument\n"); return -1; } else{ cJSON *tmp = cJSON_Parse(str); if(!tmp){ printf("Error before: [%s]\n",cJSON_GetErrorPtr()); return -1; } else{ char *symbolStr = cJSON_GetObjectItem(tmp,"dockingBay")->valuestring; if(strcmp(symbolStr,symbol1)==0){ return 1; } else if(strcmp(symbolStr,symbol2)==0){ return 2; } else if(strcmp(symbolStr,symbol3)==0){ return 3; } else{ return 0; } cJSON_Delete(tmp); } } }
struct HTTP_Response APP_Alerte(struct HTTP_Request *request) { struct HTTP_Response http_response; /* On va chercher le numero de la commande */ char *uri = "/commandes/alerte/:"; char num_cmd[3]; int int_num_cmd; strcpy(num_cmd, (request->URI + strlen(uri))); int_num_cmd = atoi(num_cmd); /* On va chercher le texte de la commande */ cJSON *bodyJson = cJSON_Parse(request->body); if (!bodyJson) { xil_printf("Error before: [%s]\n", cJSON_GetErrorPtr()); } char alerte_txt[MESSAGE_ALERTE_LEN]; JUTILS_FieldToCharArray(bodyJson, "message", alerte_txt, MESSAGE_ALERTE_LEN); cJSON_Delete(bodyJson); /* Appel de la fonction pour ajouter alerte */ CMD_Alerte(int_num_cmd, alerte_txt); //HTTP Response Okay strcpy(http_response.HTTP_ver, "HTTP/1.1"); http_response.code = 200; strcpy(http_response.description, "OK"); strcpy(http_response.content_type, "text/html; charset=utf-8"); http_response.content_length = 0; return http_response; }
// 将json文本反序列化为 url 队列 queue<url *> communication::decoding(const char * decoding_text) { assert(decoding_text!=NULL); cJSON * json_arr; cJSON * json_item; cJSON * host , * file ,* port ,* priority ,* depth; queue<url *> urlqueue; json_arr=cJSON_Parse(decoding_text); int size = cJSON_GetArraySize(json_arr); for( int i=0;i<size;i++) { json_item = cJSON_GetArrayItem(json_arr,i); if (!json_item) { printf("Error before: [%s]\n",cJSON_GetErrorPtr()); } else { //解析json item ,拼装成url, Add to urlQueue host = cJSON_GetObjectItem(json_item,"HOST"); file = cJSON_GetObjectItem(json_item,"FILE"); port = cJSON_GetObjectItem(json_item,"PORT"); priority = cJSON_GetObjectItem(json_item,"PRIORITY"); depth = cJSON_GetObjectItem(json_item,"DEPTH"); url * u = new url(host->valuestring , port->valueint ,file->valuestring , depth->valueint ,priority->valueint); urlqueue.push(u); } } cJSON_Delete(json_arr); return urlqueue; }
static gn_error parse_phonebook(char *text) { cJSON *root; root = cJSON_Parse(text); if (!root) { LOGMSG(LOG_LEVEL_CRIT, "Error before: [%s]\n",cJSON_GetErrorPtr()); return GN_ERR_FAILED; } cJSON* phonebook = cJSON_GetObjectItem(root,"phonebook"); if (!phonebook) { LOGMSG(LOG_LEVEL_CRIT, "Error: could not read phonebook object\n"); return GN_ERR_FAILED; } int i; for (i = 0;i < cJSON_GetArraySize(phonebook);i++) { cJSON *phoneItem = cJSON_GetArrayItem(phonebook,i); // handle subitem. LOGMSG(LOG_LEVEL_DEBUG, "item #%d: name=%s phone=%s\n",i, cJSON_GetObjectItem(phoneItem, "name")->valuestring, cJSON_GetObjectItem(phoneItem, "phone")->valuestring); _add_phone(cJSON_GetObjectItem(phoneItem, "name")->valuestring, cJSON_GetObjectItem(phoneItem, "phone")->valuestring); } cJSON_Delete(root); return GN_ERR_NONE; }
struct HTTP_Response APP_ChangerMotDePasse(char* bodyChar, char *authorisation) { /* Déclarer les variables locales */ struct HTTP_Response http_reponse; struct AUT_Utilisateur aut_utilisateur; unsigned char *nomPassDecode; char *nouveauMotDePasseB64; unsigned char *nouveauMotDePasse; char *token = NULL; char nouveauContenu[20000]; size_t *decsize = NULL; size_t len = strlen(authorisation); /* Decoder le contenu de champ authorisation */ nomPassDecode = b64_decode_ex(authorisation, len, decsize); /* Creer un utilisateur à partir des données décodées (champ authentification) */ token = strtok((char*) nomPassDecode, ":"); strcpy(aut_utilisateur.nomUtilisateur, token); token = strtok(NULL, "\0"); strcpy(aut_utilisateur.modDePasse, token); /* Creer un objet json a partir du texte du body */ cJSON *bodyJson = cJSON_Parse(bodyChar); if (!bodyJson) { xil_printf("Error before: [%s]\n", cJSON_GetErrorPtr()); } /* Extraire le nouveau mot de passe qui est en format base64 */ cJSON *jChild = cJSON_GetObjectItem(bodyJson, "nouveauMotDePasse"); strcpy(nouveauMotDePasseB64, jChild->valuestring ); /* on convert notre nouveau mot de passe de base64 à unsigned char */ size_t *decsizeNMDP = NULL; size_t lenNMDP = strlen(nouveauMotDePasseB64); nouveauMotDePasse = b64_decode_ex(nouveauMotDePasseB64, lenNMDP, decsizeNMDP); /* Charger la liste des utilisateurs a partir de Carte SD si ce n'est pas déjà chargé*/ int i; if(APP_ListDesUtilisateurs.nb_utilisateur == 0) { APP_ListDesUtilisateurs = AUT_ChargerListeUtilisateurAPartirCarteSD(); } /* Changer le mot de passe */ for (i = 0; i < APP_ListDesUtilisateurs.nb_utilisateur; i++) { if (strcmp(APP_ListDesUtilisateurs.utilisateurslst[i].nomUtilisateur, aut_utilisateur.nomUtilisateur) == 0 && strcmp(APP_ListDesUtilisateurs.utilisateurslst[i].modDePasse, aut_utilisateur.modDePasse) == 0) { strcpy(APP_ListDesUtilisateurs.utilisateurslst[i].modDePasse, NULL); strcpy(APP_ListDesUtilisateurs.utilisateurslst[i].modDePasse, nouveauMotDePasse); } } cJSON_Delete(bodyJson); http_reponse = APP_ActionOK(); return http_reponse; }
void _agentUpgrade(cJSON *root, s_config *config, char *http_packet) { int result; pthread_t tid_agent = 0; cJSON *valueSetObj= cJSON_CreateObject(); struct upgradeArg *ua = safe_malloc(sizeof(struct upgradeArg)); memset(ua, 0, sizeof(struct upgradeArg)); cJSON *transaction_id = cJSON_GetObjectItem(root, "transaction_id"); if(!transaction_id) { debug(LOG_ERR, "Get transaction_id faild[%s]", cJSON_GetErrorPtr()); create_http_json(valueSetObj, "0", RESPONSE, IMAGEUPGRADE, "failed", "3", "Missing parameter:{transaction_id}", config->sn, http_packet); return; } cJSON *valueSet = cJSON_GetObjectItem(root, "valueSet"); if(!valueSet) { debug(LOG_ERR, "Get valueSet faild[%s]", cJSON_GetErrorPtr()); create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, AGENTUPGRADE, "failed", "3", "Missing parameter:{valueSet}", config->sn, http_packet); return; } if(cJSON_GetObjectItem(valueSet, "file_name") == NULL || cJSON_GetObjectItem(valueSet, "md5") == NULL || !(cJSON_GetObjectItem(valueSet, "file_name")->valuestring) || !(cJSON_GetObjectItem(valueSet, "md5")->valuestring)) { debug(LOG_ERR, "Get file_name or md5 faild[%s]", cJSON_GetErrorPtr()); create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, AGENTUPGRADE, "failed", "4", "Unsupported value", config->sn, http_packet); return; } strcpy(ua->file_name, cJSON_GetObjectItem(valueSet, "file_name")->valuestring); strcpy(ua->md5, cJSON_GetObjectItem(valueSet, "md5")->valuestring); strcpy(ua->id, transaction_id->valuestring); config->upgrade_lock = 1; pthread_mutex_lock(&sMutex); result = pthread_create(&tid_agent, NULL, (void *)thread_agentUpgrade, (void *)ua); if (result != 0) { debug(LOG_ERR, "FATAL: Failed to create a new thread (thread_agentUpgrade"); create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, AGENTUPGRADE, "failed", "4", "Unsupported value", config->sn, http_packet); return; } create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, AGENTUPGRADE, "success", "0", NULL, config->sn, http_packet); }
int main(int argc, const char * argv[]) { std::cout << "Parser Initiated\n"; // std::string json = "{\n\"name\":\"Clint\",\n\"age\":99\"\n}"; // std::string json = "{ \"name\": \"Clint\" }"; char text2[]="[\"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\"]"; std::cout << text2 << std::endl; // Get C-style string from given C++ string. // Get back a pointer top-level cJSON object. cJSON* jsonRoot = cJSON_Parse(text2); if(jsonRoot == NULL) { // If the parse fails, we can request the error messages. const char * errorMessgPtr = cJSON_GetErrorPtr(); // Since errorMessgPtr is a pointer we must de-reference using the * to access // the actual char string data. std::cout << "Parse failed. Error: " << *errorMessgPtr << std::endl; // Quit with fail code. exit(EXIT_FAILURE); } /* // Pretty-print the JSON object. std::cout << "JSON OBJECT..." << std::endl; char *prettyPrintStr = cJSON_Print(jsonObject); std::cout << *prettyPrintStr << std::endl; // cJSON style recommends deallocating pretty print string. delete prettyPrintStr; // Clear pointer to NULL. prettyPrintStr = NULL; */ printJSon(jsonRoot); // std::ifstream inFile; // inFile.open("/test.json"); // std::stringstream strStream; // strStream << inFile.rdbuf(); // // std::string str = strStream.str(); // // std::cout << str << std::endl; }
void parseJsonText1(char *text1) { cJSON *root = cJSON_Parse(text1); if (root) { cJSON *format = cJSON_GetObjectItem(root, "format"); char *name = cJSON_GetObjectItem(root, "name")->valuestring; if (format) { int framerate = cJSON_GetObjectItem(format, "frame rate")->valueint; char *type = cJSON_GetObjectItem(format, "type")->valuestring; double width = cJSON_GetObjectItem(format, "width")->valuedouble; double height = cJSON_GetObjectItem(format, "height")->valuedouble; int interlace = cJSON_GetObjectItem(format, "interlace")->valueint; printf("framerate:%d\nname:%s\ntype:%s\nwidth:%lf\nheight:%lf\ninterlace:%d\n", framerate, name, type, width, height, interlace); }else{ printf("Error before: [%s]\n",cJSON_GetErrorPtr()); } cJSON_Delete(root); }else{ printf("Error before: [%s]\n",cJSON_GetErrorPtr()); } }
int http_get_dev_modeid(char *mode_id, const char *ieee) { int rt; char result_str[MAX_RESULT] = {0}; char response_json_str[MAX_RESULT] = {0}; char url_str[200]; cJSON *response_json, *json_node, *json_temp; char *temp_mode_id; snprintf(url_str, sizeof(url_str), "http://127.0.0.1/cgi-bin/rest/network/zbGetZBNodeByIEEE.cgi?" "ieee=%s&callback=1234&encodemethod=NONE&sign=AAA", ieee); rt = http_curl_get(url_str, result_str); if(rt <0) { GDGL_DEBUG("invoke api failed\n"); return -1; } get_json_str(response_json_str, result_str); response_json = cJSON_Parse(response_json_str); if (!response_json) { GDGL_DEBUG("json parse Error before: (%s)\n", cJSON_GetErrorPtr()); return -2; } json_temp = cJSON_GetObjectItem(response_json, "response_params"); if(!json_temp) { GDGL_DEBUG("cJSON_GetObjectItem failed\n"); cJSON_Delete(response_json); return -2; } json_node = cJSON_GetObjectItem(json_temp, "node"); if(!json_node) { GDGL_DEBUG("cJSON_GetObjectItem failed\n"); cJSON_Delete(response_json); return -2; } json_temp = cJSON_GetObjectItem(json_node, "model_id"); if(!json_temp) { GDGL_DEBUG("cJSON_GetObjectItem failed\n"); cJSON_Delete(response_json); return -2; } temp_mode_id = json_temp->valuestring; // printf("temp_mode_id=%s\n", temp_mode_id); snprintf(mode_id, strlen(temp_mode_id)+1, "%s", temp_mode_id); cJSON_Delete(response_json); return 0; }
void Anim_loadFromFile( Anim *a, const char *file ) { char *file_contents = NULL; cJSON *root = NULL; Byte_ReadFile( &file_contents, file ); check( file_contents, " " ); root = cJSON_Parse( file_contents ); check( root, "JSON parse error [%s] before :\n%s\n", file, cJSON_GetErrorPtr() ); cJSON *frames = cJSON_GetObjectItem( root, "frames" ); check( frames, "Animation '%s' does not have any frame!\n", file ); cJSON *frame_times = cJSON_GetObjectItem( root, "frame_times" ); check( frame_times, "Animation '%s' does not have any frame times!\n", file ); a->frame_n = cJSON_GetArraySize( frames ); int frame_t_n = cJSON_GetArraySize( frame_times ); check( a->frame_n > 0, "Animation '%s' does not have any frame!\n", file ); check( a->frame_n == frame_t_n, "Animation '%s' : entries 'frames' and 'frame_times' must have the same size!\n", file ); a->frames = byte_alloc( a->frame_n * sizeof(vec2) ); a->frame_t = byte_alloc( a->frame_n * sizeof(f32) ); for( int i = 0; i < a->frame_n; ++i ) { cJSON *frame = cJSON_GetArrayItem( frames, i ); cJSON *frame_t = cJSON_GetArrayItem( frame_times, i ); // frames positions are position on a 2048^2 texture. find their relative pos to it : if( frame ) { a->frames[i] = (vec2){ cJSON_GetArrayItem( frame, 0 )->valuedouble / 2048.f, cJSON_GetArrayItem( frame, 1 )->valuedouble / 2048.f }; a->frame_t[i] = frame_t->valuedouble; } } a->curr_t = 0.f; a->curr_n = 0; a->running = false; DEL_PTR( file_contents ); if( root ) cJSON_Delete( root ); return; error: Anim_destroy( a ); DEL_PTR( file_contents ); if( root ) cJSON_Delete( root ); }
static QVariant fromJson(const QString &string) { if (string.isEmpty()) return QVariant(); cJSON *json = cJSON_Parse(string.toUtf8().constData()); if (!json) { qWarning("Error parsing string:(%s): error: %s", qPrintable(string), cJSON_GetErrorPtr()); return QVariant(); } const QVariant variant = fromJson(json); cJSON_Delete(json); return variant; }
/* Parse text to JSON, then render back to text, and print! */ void doit(char *text) { char *out; cJSON *json; json = cJSON_Parse(text); if (!json) { printf("Error before: [%s]\n", cJSON_GetErrorPtr()); } else { out = cJSON_Print(json); cJSON_Delete(json); printf("%s\n", out); free(out); } }
void cfgReadConfig(const char *filename) { assert(cfg); filenamesModified[filename] = 0; char *content = cfgReadEntireFile(filename); if(!content) return; cJSON *root = cJSON_Parse(content); if(!root) { printf("Parse error in config file %s: %s\n", filename, cJSON_GetErrorPtr()); return; } cJSON *ns = root->child; while(ns) { if(!strcmp(ns->string, "!includes")) { cJSON *file = ns->child; while(file) { cfgReadConfig(file->valuestring); file = file->next; } } else { cJSON *attr = ns->child; while(attr) { std::string fullname = std::string(ns->string) + "." + std::string(attr->string); if(cfg->keys[fullname]) { printf("%s = %s\n", fullname.c_str(), cJSON_Print(attr)); cfg->keys[fullname]->Parse(attr); } attr = attr->next; } } ns = ns->next; } free(content); }
void parse(char * text, quote_t * quote) { char * text_part = strstr(text,"{"); cJSON * jList = cJSON_Parse(text_part); if (!jList) { printf("Error before: [%s]\n", cJSON_GetErrorPtr()); } quote_set(quote,cJSON_GetObjectItem(jList, "author")->valuestring,cJSON_GetObjectItem(jList, "quote")->valuestring); cJSON_Delete(jList); }
void _badCommand(cJSON *root, s_config *config, char *http_packet) { cJSON *valueSetObj= cJSON_CreateObject(); cJSON *transaction_id = cJSON_GetObjectItem(root, "transaction_id"); debug(LOG_DEBUG, "Invalid operation"); if(!transaction_id) { debug(LOG_ERR, "Can not find transaction_id parameter: %s", cJSON_GetErrorPtr()); create_http_json(valueSetObj, NULL, RESPONSE, "unknow", "failed", "3", "Missing parameter:{transaction_id}", config->sn, http_packet); return; } create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, "unknow", "failed", "3", "Missing parameter:{operation}", config->sn, http_packet); }
struct HTTP_Response APP_EnvoyerCode(char* bodyChar) { struct HTTP_Response http_response; int service = 0, client = 0; // Fonction qui remplit HTTP_RESPONSE AVEC UN MESSGE QUI DIT QUE CEST OKAY /* code pour client : 101*/ /* code pour service : 201*/ char fileJsonLu[8196]; readFichier("debut.txt", fileJsonLu); cJSON *bodyJson, *codeJ; char modeAEnvoyer[8196]; bodyJson = cJSON_Parse(bodyChar); if (!bodyJson) { xil_printf("Error before: [%s]\n", cJSON_GetErrorPtr()); } codeJ = cJSON_GetObjectItem(bodyJson, "code"); if (strcmp(codeJ->valuestring, APP_codeClient) == 0) { client = 101; strcpy(http_response.HTTP_ver, "HTTP/1.1"); http_response.code = 200; strcpy(http_response.description, "OK"); strcpy(http_response.content_type, "application/json"); JSON_parseDebut(fileJsonLu, "111", modeAEnvoyer); strcpy(http_response.body, modeAEnvoyer); http_response.content_length = strlen(http_response.body); } else if (strcmp(codeJ->valuestring, APP_codeWaiter) == 0) { service = 201; strcpy(http_response.HTTP_ver, "HTTP/1.1"); http_response.code = 200; strcpy(http_response.description, "OK"); strcpy(http_response.content_type, "application/json"); JSON_parseDebut(fileJsonLu, "222", modeAEnvoyer); strcpy(http_response.body, modeAEnvoyer); http_response.content_length = strlen(http_response.body); /*todo*/ } else { strcpy(http_response.HTTP_ver, "HTTP/1.1"); http_response.code = 404; strcpy(http_response.description, "Not Found"); http_response.content_length = strlen("HTTP 404 Not Found Error "); strcpy(http_response.content_type, "text/html; charset=utf-8"); strcpy(http_response.body, "HTTP 404 Not Found Error "); } return http_response; }
static int do_parsing(char *buffer) { int error; cJSON *json = cJSON_Parse(buffer); if (!json) { log_err("The JSON parser got confused around about here:"); log_err("%s", cJSON_GetErrorPtr()); return -EINVAL; } error = validate_file_type(json); if (error) return error; return xlat_is_siit() ? parse_siit_json(json) : parse_nat64_json(json); }
/* * 调api后将数据上传至云代理 * */ int invoke_api_and_upload(const char *url_str, int api_num) { time_t timestamp; // char timestamp_str[10] = {0}; int rt =0; char result_str[MAX_RESULT] = {0}; char response_json_str[MAX_RESULT] = {0}; cJSON *response_json; cJSON *new_json; char *new_json_str; timestamp = time(NULL); // snprintf(timestamp_str, sizeof(timestamp_str), "%ld", timestamp); wati_server_init_ok(); rt = http_curl_get(url_str, result_str); if(rt <0) { GDGL_DEBUG("invoke api failed\n"); return -1; } get_json_str(response_json_str, result_str); response_json = cJSON_Parse(response_json_str); if (!response_json) { GDGL_DEBUG("json parse Error before: (%s)\n", cJSON_GetErrorPtr()); return -2; } else { new_json = cJSON_CreateObject(); cJSON_AddNumberToObject(new_json, "datatype", api_num); cJSON_AddItemToObject(new_json, "data", response_json); cJSON_AddNumberToObject(new_json, "uploadtime", timestamp); new_json_str = cJSON_PrintUnformatted(new_json); // printf("new_json_str:%s, strlen:%d\n", new_json_str, strlen(new_json_str)); GDGL_DEBUG("new_json_str strlen:%d, string:%s\n", strlen(new_json_str), new_json_str); rt = upload_2_server(new_json_str, strlen(new_json_str)); free(new_json_str); cJSON_Delete(new_json); return rt; } cJSON_Delete(response_json); return rt; }
char doit_ack(char *text,const char *item_str) { char result=0;cJSON *json,*item_json; json=cJSON_Parse(text); if (!json) {printf(NET_LIB_TAG"Error ack before: [%s]\n",cJSON_GetErrorPtr());} else { item_json=cJSON_GetObjectItem(json,item_str); if((item_json->type & 255) ==cJSON_True) { result=1; } cJSON_Delete(json); } return result; }
void mono_dojson(const char *jtext) { char *out; cJSON *json; json=cJSON_Parse(jtext); if (!json) { mono_info("Error before: [%s]\n",cJSON_GetErrorPtr()); } else { out = cJSON_Print(json); cJSON_Delete(json); mono_info("%s\n",out); FREE(out); } }