2017-09-26 94 views
0

我使用從我的.get(電影)函數接收的json值來獲取我的json電影對象中每個鍵的值。我試圖將它輸出到fltk GUI中的字段中,該字段需要是const char *類型。但是,我得到了奇怪的字符,而不是我的價值觀。這裏有一個明顯的問題嗎?從json C++輸出獲取字段中的奇怪字符

Json::Value result = m.get(movie); 
std::cout << result << endl; 
const char *released = result.get("Released", "NULL").asCString(); 
releasedInput->value(released); 
const char *rated = result.get("Rated", "NULL").asCString(); 
ratedInput->value(rated); 
Json::Value actors = result.operator[]("Actors"); 
const char *plot = result.get("Plot", "NULL").asCString(); 
plotMLIn->value(plot); 
const char *runtime = result.get("Runtime", "NULL").asCString(); 
runtimeInput->value(runtime); 
Json::Value genre = result.operator[]("Genre"); 
const char *filename = result.get("Filename", "NULL").asCString(); 
filenameInput->value(filename); 
const char *title = result.get("Title", "NULL").asCString(); 
titleInput->value(title) 

我在我的函數中只粘貼了相關行。如果需要更多澄清,我很樂意提供。

+0

您是否嘗試過調試代碼?嘗試在調試器中逐行執行代碼,同時監視變量及其值?什麼字符串錯了? *他們怎麼錯了?你期望什麼文字,你實際上得到了什麼? –

+0

對於#1,當我使用std :: string時,我得到了錯誤消息,他們必須是類型const char *,所以我改變了它。對於#2,這是因爲我只是在學習這一點,並且從文檔看,這看起來是該做的事情。 – Kendra

+0

哦,你爲什麼要顯式調用'operator []'作爲一個函數?爲什麼不'結果[ 「演員」]'? –

回答

1

您應該將結果保存在std::string中,然後在該字符串上調用c_str()以獲取C字符串。如果您將這些調用鏈接起來並立即保存指針,或者只保存C字符串指向的內存的字符串對象將被清除,您將在代碼中調用未定義的行爲,這不是您想要的。

std::string runtime = result.get("Runtime", "NULL").asString(); 
runtimeInput->value(runtime.c_str()); 
0

我明白了。我只需要改變.asCString(); asString()。c_str();

我敢肯定,這樣做更有說服力,但正如我所說,我是一個新手。

+0

這隻會顯示工作,它仍然是未定義的行爲。 –