2017-10-17 27 views
1

我有期望的配置在以下JSON格式但精縮的應用程序:分配[空]以JSON ::在JsonCpp

<config-json> 
{ 
    "config" : { 
     "services" : { 
      "analytics" : { 
       "sensor" : [ 
       { 
        "name" : "ip-sensor", 
        "server-name" : ["ip-server1"], 
        "export-name" : "ip-export1", 
        "resource" : "/ipv4", 
        "bulk" : [null] // <-- Notice 
       } 
       ] 
      } 
     } 
    } 
} 
</config-json> 

在上述JSON配置,該應用預計"bulk"是爲[null]總是。從應用程序的角度來看,這是一個正確的期望。

在我的配置生成器代碼中,我使用JsonCpp通過使用Json::Value來構建JSON對象。

因爲"bulk"需求爲[null],我填充它,如下所示:

//Json::Value *json_obj //Getting passed as an arg 
(*json_obj)["config"]["services"]["analytics"]["sensor"][0]["bulk"] = Json::nullValue; 

但我得到的是:

"bulk":null // Notice the missing [] around null.

,因此配置被丟棄。

是否有JsonCpp辦法做到以下幾點:

"bulk" : [null] 

回答

2

由於JSON中括號內的數組,你要去的是包含空數組。您應該可以這樣做:

Json::Value jsonArray; 
jsonArray.append(Json::Value::null); 
(*json_obj)["config"]["services"]["analytics"]["sensor"][0][‌​"bulk"] = jsonArray; 
+0

也可以用作'(* json_obj)[「config」] [「services」] [「analytics」] [「sensor」] [0] [ 「bulk」]。append(Json :: Value :: null);' 不是? –