2015-11-06 50 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对于此代码