How to serialize a JSON key containing dots (e.g. IP address) using SuperObject?

I am trying to save JSON where IP is the key. Expected JSON Result:

{"SnmpManagers":[{"10.112.25.235":162}]} 

Delphi SuperObject Code:

 const IpAddr = '10.112.25.235'; Port = 162; var tmp: TSuperObject; begin tmp := TSuperObject.Create; tmp.I[IpAddr] := Port; Json.A['SnmpManagers'].Add(tmp); end; 

SuperObject parses points as path separators for a JSON object:

 {"SnmpManagers":[{"10":{"112":{"25":{"235":162}}}}]} 

How to save IP address as JSON key using SuperObject?

+6
source share
2 answers

The solution is to create a JSON object from a string

 Json.A['SnmpManagers'].Add(SO(Format('{"%s":%d}', [IpAddr, Port]))); 

Another way to add (do not use with .O [], because AsObject gives zero for non-existing keys):

 // for a simple key-value object Json.AsObject.S['1.2.3'] := 'a'; // gives us {{"1.2.3":"a"}} Json.AsObject.S['4.5'] := 'b'; // gives us {{"1.2.3":"a"}, {"4.5":"b"}} 
+6
source

This also works:

 var tmp: ISuperObject; begin tmp := SO([IpAddr, port]); Json.A['SnmpManagers'].Add(tmp); 
+1
source

All Articles