I need to build a nested JSON string as in the example below. I have it working with the old framework but as I am rewriting the corresponding program, I would like to make use of the new framework now.
As far as I see, there is no equivalent to JSONItem in the new framework and I wonder how I have to add a json to json?
Further I wonder how to add a list in square brackets, like the PaymentMethods you can see in the example below.
I have not found an example in the Dev Center (http://developer.xojo.com/xojo-data). Am I overlooking something or is it not documented?
Can anyone give me some example how to create such a nested string with the new framework?
{
"RequestHeader": {
"SpecVersion": "1.3",
"CustomerId": "123123",
"RequestId": "33e8af17-35c1-4165-a343-c1c86a320f3b",
"RetryIndicator": 0,
"ClientInfo": {
"ShopInfo": "My Shop",
"ApplicationInfo": "ApplicationInfo",
"OsInfo": "Windows Server 2013"
}
},
"TerminalId": "12345678",
"Payment": {
"Amount": {
"Value": "100",
"CurrencyCode": "CHF"
},
"OrderId": "OrderId",
"Description": "Description",
"PayerNote": "Payernote"
},
"PaymentMethods": [
"VISA",
"MASTERCARD",
"MASTERPASS"
],
"Payer": {
"IpAddress": "111.111.111.111"
},
"ReturnUrls": {
"Success": "https://merchanthost/success",
"Fail": "https://merchanthost/fail",
"Abort": "https://merchanthost/abort"
},
"Notification": {
"MerchantEmail": "merchant@saferpay.com",
"NotifyUrl": "https://merchanthost/notify.aspx?ID=ABC123"
}
}
Use Xojo.Core.Dictionary and Auto() (or other valid array types like Boolean() or Text()) to create your structure, then Xojo.Data.GenerateJSON to convert to a JSON string.
Part of your example:
dim parent as new Xojo.Core.Dictionary
dim child as new Xojo.Core.Dictionary
child.Value( "SpecVersion" ) = "1.3"
child.Value( "CustomerId" ) = "123123"
parent.Value( "RequestHeader" ) = child
dim payMethods() as text = array( "VISA", "MASTERCARD" )
parent.Value( "PaymentMethods" ) = payMethods
dim json as text = Xojo.Data.GenerateJSON( parent )
Note: with small structures it won’t matter, but for larger ones you’ll find the new framework significantly faster on the Mac and significantly slower on Windows.
Thanks a lot, Kem!
I got confused with the new way of naming things.
I would like to see this sample code of yours in the documentation.
Are there any known Json speed differences on Linux? My code will run on a Linux server…
I don’t recall if I tested, and don’t think so, but you can check it out using the project in this report:
<https://xojo.com/issue/45229>
One more note: if you need to add an array of object to your JSON, you might be tempted (as I once was) to do this:
dim arr() as Xojo.Core.Dictionary
arr.Append new Xojo.Core.Dictionary
parent.Value( "myObjects" ) = arr
This will generate an exception when you try to convert to JSON. Make your arrays one of these:
- String()
- Text()
- Double()
- Integer() // Any kind of int, I think
- Boolean()
- Auto()
Also, if you need case-sensitivity to start, create your initial dictionaries this way:
dim parent as Xojo.Core.Dictionary = Xojo.Data.ParseJSON( "{}" )
That’s an easy way to return a case-sensitive Dictionary.
[quote=303977:@Kem Tekinay]I don’t recall if I tested, and don’t think so, but you can check it out using the project in this report:
<https://xojo.com/issue/45229>[/quote]
I recreated this test program as a webapp, 64-bit for Linux, and I am testing it on a Ubuntu Server 14.04 VPS
(12GB real memory, 6 GB virtual memory, Intel Xeon CPU E5520 @ 2.27GHz, 10 cores)
The results Im getting:
51.061 seconds
50.129 seconds
51.376 seconds
(IMHO : This is freakin horrible. This takes about 0.7 seconds on a Mac and almost 9 seconds on Windows, desktop)
I added the test webapp to the feedback case