簡體   English   中英

Json Cpp isMember()總是返回True

[英]Json Cpp isMember() Returns True always

例如在簡單的json中

{
    "A" :
    {
       "B" :
       {
         --something--
       }
    }
}

第一個案例:

json::Value root;
const Json::Value x = root["A"]["B"];
if (root.isMember("A")) --- always returns TRUE..

第二種情況:

Json::Value root;
If (root.isMember("A"))  ---- works fine
const Json::Value x = root["A"]["B"]; 

知道First Case有什么問題嗎? 即使我在isMember()調用之前得到x

看看文檔

Value &     operator[] (const char *key)
    Access an object value by name, create a null member if it does not exist. 
const Value &   operator[] (const char *key) const
    Access an object value by name, returns null if there is no member with that name. 
Value &     operator[] (const std::string &key)
    Access an object value by name, create a null member if it does not exist. 
const Value &   operator[] (const std::string &key) const
    Access an object value by name, returns null if there is no member with that name. 

基本上,您在root["A"]調用上創建成員"A" 為了避免這種isMember在實際訪問成員之前總是檢查isMember (或者只在const對象上調用它,而不是執行null檢查 - 我會預先設置前者)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM