本文整理汇总了C++中cJSON_Parse函数的典型用法代码示例。如果您正苦于以下问题:C++ cJSON_Parse函数的具体用法?C++ cJSON_Parse怎么用?C++ cJSON_Parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了cJSON_Parse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: receiveWriteRequest
bool receiveWriteRequest(uint8_t* message) {
cJSON *root = cJSON_Parse((char*)message);
bool foundMessage = false;
if(root != NULL) {
foundMessage = true;
cJSON* nameObject = cJSON_GetObjectItem(root, "name");
if(nameObject == NULL) {
cJSON* idObject = cJSON_GetObjectItem(root, "id");
if(idObject == NULL) {
debug("Write request is malformed, "
"missing name or id: %s", message);
} else {
receiveRawWriteRequest(idObject, root);
}
} else {
receiveTranslatedWriteRequest(nameObject, root);
}
cJSON_Delete(root);
} else {
debug("No valid JSON in incoming buffer yet -- "
"if it's valid, may be out of memory");
}
return foundMessage;
}
示例2: get_coin_info
char *SuperNET_JSON(char *JSONstr)
{
char *retstr = 0;
struct coin_info *cp = get_coin_info("BTCD");
cJSON *json;
if ( Finished_init == 0 )
return(0);
if ( Debuglevel > 1 )
printf("got JSON.(%s)\n",JSONstr);
if ( cp != 0 && (json= cJSON_Parse(JSONstr)) != 0 )
{
if ( 1 && is_BTCD_command(json) != 0 ) // deadlocks as the SuperNET API came from locked BTCD RPC
{
//if ( Debuglevel > 1 )
// printf("is_BTCD_command\n");
return(block_on_SuperNET(0,JSONstr));
}
else retstr = block_on_SuperNET(1,JSONstr);
free_json(json);
} else printf("couldnt parse (%s)\n",JSONstr);
if ( retstr == 0 )
retstr = clonestr("{\"result\":null}");
return(retstr);
}
示例3: dir_fopen_warn
cJSON *load_json( const char *filename )
{
#ifdef ENABLE_CONFIGURATION
FILE *f = dir_fopen_warn(get_user_directory(), filename, "rb");
if (f == NULL)
return NULL;
size_t buffer_len = ftell_eof(f);
char *buffer = malloc(buffer_len + 1);
fread(buffer, 1, buffer_len, f);
buffer[buffer_len] = '\0';
fclose(f);
cJSON *root = cJSON_Parse(buffer);
free(buffer);
return root;
#else
return NULL;
#endif /*ENABLE_CONFIGURATION*/
}
示例4: parse
void parse(direct ** dir)
{
FILE * file = fopen("director.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 * birthdate = cJSON_GetObjectItem(jItem, "birthdate")->valuestring;
char * company = cJSON_GetObjectItem(jItem, "company")->valuestring;
char * name_com = cJSON_GetObjectItem(jItem, "name_com")->valuestring;
char * specialization = cJSON_GetObjectItem(jItem, "specialization")->valuestring;
int year = cJSON_GetObjectItem(jItem, "year")->valueint;
double rating = cJSON_GetObjectItem(jItem, "rating")->valuedouble;
Director_set(Director[i], name, surname, birthdate, company, name_com, specialization, year, rating);
}
cJSON_Delete(jList);
}
示例5: UpdatePersistentStorage
/**
* Function to update persistent storage
*/
static bool UpdatePersistentStorage(OicSecPstat_t * pstat)
{
bool bRet = false;
if (pstat)
{
// Convert pstat data into JSON for update to persistent storage
char *jsonStr = BinToPstatJSON(pstat);
if (jsonStr)
{
cJSON *jsonPstat = cJSON_Parse(jsonStr);
OICFree(jsonStr);
if (jsonPstat &&
(OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_PSTAT_NAME, jsonPstat)))
{
bRet = true;
}
cJSON_Delete(jsonPstat);
}
}
return bRet;
}
示例6: append_stdfields
static void append_stdfields(char *retbuf,int32_t max,struct plugin_info *plugin,uint64_t tag,int32_t allfields)
{
char tagstr[512]; cJSON *json; int32_t len;
//printf("APPEND.(%s) (%s)\n",retbuf,plugin->name);
tagstr[0] = 0;
len = (int32_t)strlen(retbuf);
if ( len > 4 && retbuf[len-1] != ']' && (json= cJSON_Parse(retbuf)) != 0 )
{
if ( tag != 0 && get_API_nxt64bits(cJSON_GetObjectItem(json,"tag")) == 0 )
sprintf(tagstr,",\"tag\":\"%llu\"",(long long)tag);
if ( cJSON_GetObjectItem(json,"serviceNXT") == 0 && plugin->SERVICENXT[0] != 0 )
sprintf(tagstr+strlen(tagstr),",\"serviceNXT\":\"%s\"",plugin->SERVICENXT);
if ( cJSON_GetObjectItem(json,"NXT") == 0 )
sprintf(tagstr+strlen(tagstr),",\"NXT\":\"%s\"",plugin->NXTADDR);
if ( allfields != 0 )
{
//if ( SUPERNET.iamrelay != 0 )
// sprintf(retbuf+strlen(retbuf)-1,",\"myipaddr\":\"%s\"}",plugin->ipaddr);
sprintf(retbuf+strlen(retbuf)-1,",\"allowremote\":%d%s}",plugin->allowremote,tagstr);
sprintf(retbuf+strlen(retbuf)-1,",\"permanentflag\":%d,\"daemonid\":\"%llu\",\"myid\":\"%llu\",\"plugin\":\"%s\",\"endpoint\":\"%s\",\"millis\":%.2f,\"sent\":%u,\"recv\":%u}",plugin->permanentflag,(long long)plugin->daemonid,(long long)plugin->myid,plugin->name,plugin->bindaddr[0]!=0?plugin->bindaddr:plugin->connectaddr,milliseconds2(),plugin->numsent,plugin->numrecv);
}
else sprintf(retbuf+strlen(retbuf)-1,",\"daemonid\":\"%llu\",\"myid\":\"%llu\",\"allowremote\":%d%s}",(long long)plugin->daemonid,(long long)plugin->myid,plugin->allowremote,tagstr);
}
}
示例7: qDebug
void SocketService::logipslots(const QString &vmid)
{
QString params = "&hostId="+ vmid;
std::string hostid = params.toLocal8Bit().data();
std::string hostmac = "&boxId=" + boxmac;
std::string hoststatus = "&status=" + vmstatus;
std::string httpinfo = httphead + "iChange";
std::string checkinfo = userpass + hostid + hostmac + hoststatus;
std::string strmsg = "";
qDebug()<<"checkinfo:"<<checkinfo.c_str();
http->Post(httpinfo, checkinfo, strmsg);
qDebug() << "logip:"<<strmsg.c_str();
cJSON *json = cJSON_Parse(strmsg.data());
if(json == NULL)
{
return;
}
cJSON *json_result = cJSON_GetObjectItem(json,"result");
if(json_result == NULL)
{
emit signals_httperror("JSON Pare error!");
return ;
}
QString result = json_result->valuestring;
if(result.compare("false") == 0)
{
cJSON *json_error = cJSON_GetObjectItem(json,"errorMsg");
emit signals_httperror(json_error->valuestring);
}
cJSON_Delete(json);
}
示例8: parse
size_t parse(char *ptr, size_t size, size_t nmemb, char *data)
{
if(size*nmemb <= 2)
{
return size*nmemb;
}
printf("%s\n", ptr); //DEBUG
cJSON *root = cJSON_Parse(ptr);
struct cJSON *current = root->child;
while(strcmp(current->string, "text") != 0)
{
if(current->next != NULL)
{
current = current->next;
} else {
break;
printf("done"); //DEBUG
}
}
if(strstr(current->valuestring, "START") != NULL)
{
bcm2835_gpio_write(PIN, HIGH);
} else if(strstr(current->valuestring, "OFF") != NULL)
{
bcm2835_gpio_write(PIN, LOW);
}
printf("Current object: %s\n", cJSON_Print(current)); //DEBUG
return size*nmemb;
}
示例9: jprint
char *PARSEBALANCE(struct exchange_info *exchange,double *balancep,char *coinstr)
{
char *itemstr = 0; cJSON *item,*obj; double onorders,btcvalue;
*balancep = 0.;
if ( exchange->balancejson != 0 && (item= jobj(exchange->balancejson,coinstr)) != 0 )
{
itemstr = jprint(item,0);
*balancep = jdouble(item,"available");
onorders = jdouble(item,"onOrders");
btcvalue = jdouble(item,"btcValue");
if ( (obj= cJSON_Parse(itemstr)) != 0 )
{
free(itemstr);
jaddstr(obj,"base",coinstr);
jaddnum(obj,"balance",*balancep);
jaddnum(obj,"onOrders",onorders);
jaddnum(obj,"btcvalue",btcvalue);
itemstr = jprint(obj,1);
}
}
if ( itemstr == 0 )
return(clonestr("{\"error\":\"cant find coin balance\"}"));
return(itemstr);
}
示例10: UpdatePersistentStorage
/**
* @todo document this function including why code might need to call this.
* The current suspicion is that it's not being called as much as it should.
*/
static bool UpdatePersistentStorage(OicSecDoxm_t * doxm)
{
bool bRet = false;
if (NULL != doxm)
{
// Convert Doxm data into JSON for update to persistent storage
char *jsonStr = BinToDoxmJSON(doxm);
if (jsonStr)
{
cJSON *jsonDoxm = cJSON_Parse(jsonStr);
OICFree(jsonStr);
if (jsonDoxm &&
(OC_STACK_OK == UpdateSVRDatabase(OIC_JSON_DOXM_NAME, jsonDoxm)))
{
bRet = true;
}
cJSON_Delete(jsonDoxm);
}
}
return bRet;
}
示例11: platform_load
int platform_load(int type, const char *proto)
{
assert(proto);
std::fstream fs(proto, std::ios::in | std::ios::binary);
if (!fs) {
LOG_ERROR("json",
"Can NOT open file %s.", proto);
return -1;
}
std::stringstream iss;
iss << fs.rdbuf();
cJSON *json = cJSON_Parse(iss.str().c_str());
if (json == NULL) {
LOG_ERROR("json",
"Can NOT parse json file %s.", proto);
return -1;
}
s_jsons.insert(std::make_pair<int, cJSON*>(type, json));
return 0;
}
示例12: exchange_nonce
cJSON *SIGNPOST(char **retstrp,struct exchange_info *exchange,char *payload,char *path)
{
static CURL *cHandle;
char url[1024],req[1024],md5secret[128],tmp[1024],dest[1025],hdr1[512],hdr2[512],hdr3[512],hdr4[512],*sig,*data = 0;
cJSON *json; uint64_t nonce;
hdr1[0] = hdr2[0] = hdr3[0] = hdr4[0] = 0;
json = 0;
nonce = exchange_nonce(exchange) * 1000 + ((uint64_t)milliseconds() % 1000);
sprintf(tmp,"%llu%s%s",(long long)nonce,exchange->userid,exchange->apikey);
calc_md5(md5secret,exchange->apisecret,(int32_t)strlen(exchange->apisecret));
if ( (sig= hmac_sha256_str(dest,md5secret,(int32_t)strlen(md5secret),tmp)) != 0 )
{
sprintf(req,"{\"key\":\"%s\",%s\"nonce\":%llu,\"signature\":\"%s\"}",exchange->apikey,payload,(long long)nonce,sig);
sprintf(hdr1,"Content-Type:application/json"), sprintf(hdr2,"charset=utf-8"), sprintf(hdr3,"Content-Length:%ld",(long)strlen(req));
sprintf(url,"https://api.quadrigacx.com/v2/%s",path);
if ( (data= curl_post(&cHandle,url,0,req,hdr1,hdr2,hdr3,hdr4)) != 0 )
json = cJSON_Parse(data);
}
if ( retstrp != 0 )
*retstrp = data;
else if ( data != 0 )
free(data);
return(json);
}
示例13: do_get_list
cJSON* do_get_list()
{
wchar header[128];
const wchar_t* arr[1];
arr[0] = header;
wchar url[128];
char buf[128];
cl::StringUtil::format(buf, 128, "Authorization: %s\r\n", g_token);
cl::StringUtil::char_to_wchar(header, 128, buf);
cl::StringUtil::format(buf, 128, "http://%s/v2/instances", g_host);
cl::StringUtil::char_to_wchar(url, 128, buf);
char* response;
int len;
unsigned long code;
int ret = urlRequest(url, L"GET", arr, 1, NULL, 0, &code, &response, &len);
if(ret)
{
if(code == 200)
{
cJSON* root = cJSON_Parse(response);
cl_free(response);
return root;
}
else
{
cl_free(response);
return NULL;
}
}
return NULL;
}
示例14: ConvertJsonToCBOR
static void ConvertJsonToCBOR(const char *jsonFileName, const char *cborFileName)
{
char *jsonStr = NULL;
FILE *fp = NULL;
FILE *fp1 = NULL;
uint8_t *aclCbor = NULL;
uint8_t *pstatCbor = NULL;
uint8_t *doxmCbor = NULL;
uint8_t *amaclCbor = NULL;
uint8_t *svcCbor = NULL;
uint8_t *credCbor = NULL;
cJSON *jsonRoot = NULL;
OCStackResult ret = OC_STACK_ERROR;
size_t size = GetJSONFileSize(jsonFileName);
if (0 == size)
{
OIC_LOG (ERROR, TAG, "Failed converting to JSON");
return;
}
jsonStr = (char *)OICMalloc(size + 1);
VERIFY_NON_NULL(TAG, jsonStr, FATAL);
fp = fopen(jsonFileName, "r");
if (fp)
{
size_t bytesRead = fread(jsonStr, 1, size, fp);
jsonStr[bytesRead] = '\0';
OIC_LOG_V(DEBUG, TAG, "Read %zu bytes", bytesRead);
fclose(fp);
fp = NULL;
}
else
{
OIC_LOG (ERROR, TAG, "Unable to open JSON file!!");
goto exit;
}
jsonRoot = cJSON_Parse(jsonStr);
cJSON *value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_ACL_NAME);
//printf("ACL json : \n%s\n", cJSON_PrintUnformatted(value));
size_t aclCborSize = 0;
if (NULL != value)
{
OicSecAcl_t *acl = JSONToAclBin(jsonStr);
VERIFY_NON_NULL(TAG, acl, FATAL);
ret = AclToCBORPayload(acl, &aclCbor, &aclCborSize);
if(OC_STACK_OK != ret)
{
OIC_LOG (ERROR, TAG, "Failed converting Acl to Cbor Payload");
DeleteACLList(acl);
goto exit;
}
printf("ACL Cbor Size: %zd\n", aclCborSize);
DeleteACLList(acl);
}
value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_PSTAT_NAME);
size_t pstatCborSize = 0;
if (NULL != value)
{
OicSecPstat_t *pstat = JSONToPstatBin(jsonStr);
VERIFY_NON_NULL(TAG, pstat, FATAL);
ret = PstatToCBORPayload(pstat, &pstatCbor, &pstatCborSize);
if(OC_STACK_OK != ret)
{
OIC_LOG (ERROR, TAG, "Failed converting Pstat to Cbor Payload");
DeletePstatBinData(pstat);
goto exit;
}
printf("PSTAT Cbor Size: %zd\n", pstatCborSize);
DeletePstatBinData(pstat);
}
value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_DOXM_NAME);
size_t doxmCborSize = 0;
if (NULL != value)
{
OicSecDoxm_t *doxm = JSONToDoxmBin(jsonStr);
VERIFY_NON_NULL(TAG, doxm, FATAL);
ret = DoxmToCBORPayload(doxm, &doxmCbor, &doxmCborSize);
if(OC_STACK_OK != ret)
{
OIC_LOG (ERROR, TAG, "Failed converting Doxm to Cbor Payload");
DeleteDoxmBinData(doxm);
goto exit;
}
printf("DOXM Cbor Size: %zd\n", doxmCborSize);
DeleteDoxmBinData(doxm);
}
value = cJSON_GetObjectItem(jsonRoot, OIC_JSON_AMACL_NAME);
size_t amaclCborSize = 0;
if (NULL != value)
{
OicSecAmacl_t *amacl = JSONToAmaclBin(jsonStr);
VERIFY_NON_NULL(TAG, amacl, FATAL);
ret = AmaclToCBORPayload(amacl, &amaclCbor, &amaclCborSize);
if(OC_STACK_OK != ret)
{
//.........这里部分代码省略.........
示例15: JSONToCredBin
OicSecCred_t * JSONToCredBin(const char * jsonStr)
{
if (NULL == jsonStr)
{
OIC_LOG(ERROR, TAG,"JSONToCredBin jsonStr in NULL");
return NULL;
}
OicSecCred_t *headCred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
OCStackResult ret = OC_STACK_ERROR;
cJSON *jsonRoot = NULL;
VERIFY_NON_NULL(TAG, headCred, ERROR);
jsonRoot = cJSON_Parse(jsonStr);
VERIFY_NON_NULL(TAG, jsonRoot, ERROR);
cJSON *jsonCredMap = cJSON_GetObjectItem(jsonRoot, OIC_JSON_CRED_NAME);
VERIFY_NON_NULL(TAG, jsonCredMap, ERROR);
// creds
cJSON *jsonCredArray = NULL;
jsonCredArray = cJSON_GetObjectItem(jsonCredMap, OIC_JSON_CREDS_NAME);
VERIFY_NON_NULL(TAG, jsonCredArray, ERROR);
if (cJSON_Array == jsonCredArray->type)
{
int numCred = cJSON_GetArraySize(jsonCredArray);
VERIFY_SUCCESS(TAG, numCred > 0, ERROR);
int idx = 0;
do
{
cJSON *jsonCred = cJSON_GetArrayItem(jsonCredArray, idx);
VERIFY_NON_NULL(TAG, jsonCred, ERROR);
OicSecCred_t *cred = NULL;
if(idx == 0)
{
cred = headCred;
}
else
{
cred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
OicSecCred_t *temp = headCred;
while (temp->next)
{
temp = temp->next;
}
temp->next = cred;
}
VERIFY_NON_NULL(TAG, cred, ERROR);
size_t jsonObjLen = 0;
cJSON *jsonObj = NULL;
//CredId -- Mandatory
jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDID_NAME);
if(jsonObj)
{
VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
cred->credId = jsonObj->valueint;
}
//subject -- Mandatory
jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_SUBJECTID_NAME);
VERIFY_NON_NULL(TAG, jsonObj, ERROR);
VERIFY_SUCCESS(TAG, cJSON_String == jsonObj->type, ERROR);
ret = ConvertStrToUuid(jsonObj->valuestring, &cred->subject);
VERIFY_SUCCESS(TAG, OC_STACK_OK == ret, ERROR);
//CredType -- Mandatory
jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_CREDTYPE_NAME);
VERIFY_NON_NULL(TAG, jsonObj, ERROR);
VERIFY_SUCCESS(TAG, cJSON_Number == jsonObj->type, ERROR);
cred->credType = (OicSecCredType_t)jsonObj->valueint;
//PrivateData is mandatory for some of the credential types listed below.
jsonObj = cJSON_GetObjectItem(jsonCred, OIC_JSON_PRIVATEDATA_NAME);
if (NULL != jsonObj)
{
cJSON *jsonPriv = cJSON_GetObjectItem(jsonObj, OIC_JSON_DATA_NAME);
VERIFY_NON_NULL(TAG, jsonPriv, ERROR);
jsonObjLen = strlen(jsonPriv->valuestring);
cred->privateData.data = (uint8_t *)OICCalloc(1, jsonObjLen);
VERIFY_NON_NULL(TAG, (cred->privateData.data), ERROR);
memcpy(cred->privateData.data, jsonPriv->valuestring, jsonObjLen);
cred->privateData.len = jsonObjLen;
cJSON *jsonEncoding = cJSON_GetObjectItem(jsonObj, OIC_JSON_ENCODING_NAME);
VERIFY_NON_NULL(TAG, jsonEncoding, ERROR);
if(strcmp(OIC_SEC_ENCODING_RAW, jsonEncoding->valuestring) == 0)
{
cred->privateData.encoding = OIC_ENCODING_RAW;
}
else if(strcmp(OIC_SEC_ENCODING_BASE64, jsonEncoding->valuestring) == 0)
{
cred->privateData.encoding = OIC_ENCODING_BASE64;
}
else
{
//.........这里部分代码省略.........