2017-10-17 1 views
1

ich eine Anwendung, die config in folgenden JSON-Format erwartet, aber minimierte:zuordnen [null] in Json :: Wert in 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> 

In der obigen JSON config die App erwartet "bulk" als [null] immer zu sein . Und das ist eine richtige Erwartung aus Sicht der App.

In meinem Config-Generator-Code verwende ich JsonCpp, um die JSON-Objekte mit Json::Value zu erstellen.

Da "bulk" Bedürfnisse [null] sein, ich bin bevölkern es wie folgt:

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

Aber was ich bekommen ist:

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

Und daher wird die Config verworfen.

Gibt es eine Möglichkeit in JsonCpp folgendes zu erreichen:

"bulk" : [null] 

Antwort

2

Da Klammern in Json ein Array bezeichnen, was Sie gehen ist ein Array mit null. Sie sollten die Lage zu tun, dass in etwa so:

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

auch als '(* json_obj) [ "config"] [ "services"] [ "Analytik"] [ "Sensor"] verwendet werden kann [0] [ "Bulk"]. Append (Json :: Wert :: null); ' Nein? –

Verwandte Themen