2015-11-06 52 views
0

我正在嘗試一些應該很簡單的事情。我想從Mongo中返回一個返回的JSON值,並驗證這個值是我期待的。爲此,我使用json_object_to_json_string返回json輸出的字符串值。json_object_to_json_string在'c'中返回值WITHOUT引號

問題是字符串正在用雙引號圍繞該值返回:返回EX「562416504bacd3940b8b2d5c」而不是562416504bacd3940b8b2d5c。

然後這阻止我能夠做一個簡單的匹配(見下文)。

反正有沒有惱人的雙引號得到json值?

struct json_object *new_obj; 

// Fetch document 
while (mongoc_cursor_next (cursor, &doc)) 
{ 
char *docAsJSON = bson_as_json (doc, NULL); 

// Grab account_id from session table 
new_obj = json_tokener_parse(docAsJSON); 
new_obj = json_object_object_get(new_obj, "account_id"); 
char * account_id_fromSession = json_object_to_json_string(new_obj); 

if (strcmp(account_id,account_id_fromSession) == 0) 
    { 
    printf("\nids are the same\n\n"); 
    } 
else 
    { 
    printf("\nids are NOT the same %s %s\n\n",account_id,account_id_fromSession); 
    } 

輸出的代碼:

ids are NOT the same 562416504bacd3940b8b2d5c "562416504bacd3940b8b2d5c" 

回答

0

我有一個 「貧民窟」 的解決方案,但想的東西比一個字符串替換更優雅

void remove_all_chars(char* str, char c) 
{ 
char *pr = str, *pw = str; 
while (*pr) 
    { 
    *pw = *pr++; 
    pw += (*pw != c); 
    } 
*pw = '\0'; 
} 

// Remove " from token 
remove_all_chars(account_id_fromSession, '"'); 

由於通過另一個問題@dasblinkenlight對於此代碼