[Gambas-user] Encode JSON form a collection within a collection?
Admin
admin at allunix.ru
Sun Feb 4 06:12:33 CET 2024
02.02.2024 0:20, T Lee Davidson пишет:
> On 1/31/24 23:06, Admin wrote:
>> Greetings!
>>
>> I decided to make an SMS notifications for myself, chose a provider
>> and read the API documentation. It's pretty straight-forward, they
>> just want to recieve a JSON push like this:
>>
>> {
>> "destination": "phone",
>> "originator": "alpha name",
>> "body": "message text",
>> "msisdn": "972000000",
>> "reference": "ext_id_16",
>> "validity": "1",
>> "tariff": "0",
>> "callback_url": "your_url"
>> }
>>
>> So I wrote a code:
>>
>> Public Sub SendSMS(sText As String, num As String)
>> Dim hClient As New HttpClient
>> Dim restapi As New Collection
>> Dim headers As New String[]
>>
>> headers.Add("X-API-KEY: whatevermykeyis")
>> headers.Add("accept: application/json")
>>
>> restapi.Add("phone", "destination")
>> restapi.Add("Me", "originator")
>> restapi.Add(sText, "body")
>> restapi.Add(num, "msisdn")
>> restapi.Add("ext_id_" & Int(Rnd(1, 9)) & CInt(Now), "reference")
>>
>> hClient.URL = "https://api.myprovider.url/sms/create"
>> hClient.Post("application/json", JSON.Encode(restapi), headers,
>> "~/sms.log")
>> End
>>
>> And it works, no problem. I was happy till I had to send the same SMS
>> to more (a lot more) people. I can just pass 'num' for each number I
>> want and send separate SMSes, which I do, but it's expensive. The
>> prodiver can send up to 50 SMSes for the price of just one if I use
>> another push request:
>>
>> {
>> "validity": "1",
>> "tariff": "0",
>> "destination": "phones",
>> "originator": "alpha_name",
>> "body": "message text",
>> "phones":
>> [
>> {
>> "msisdn": "972000000",
>> "reference": "ext_id_17"
>> },
>> {
>> "msisdn": "972000001",
>> "reference": "ext_id_18"
>> }
>> ],
>> "callback_url": "your_url"
>> }
>>
>>
>> And here I'm a bit stuck. How do I encode an array, a collection
>> maybe, into a JSON which already is a collection?
>>
>>
>> Dmitry.
>
> Benoit's answer provides a simple and straightforward solution. But,
> it is hard coded.
>
> A more flexible solution takes advantage of the fact that Collections
> can be nested and that there exists a Collection[] array.
>
> [code]
> ' Gambas module file
>
> Public Sub Main()
> Dim sRecipients As String[] = ["972000000", "972000001"]
> Dim phones As New Collection[] ' array of Collections
>
> For Each num As String In sRecipients
> phones.Add(["msisdn": num, "reference": "ext_id_" & Int(Rnd(1, 9))
> & CInt(Now)])
> Next
>
> SendMultipleSMS("My message", phones)
> End
>
> Public Sub SendMultipleSMS(sText As String, phones As Collection[])
> Dim hClient As New HttpClient
> Dim restapi As New Collection
> Dim headers As New String[]
>
> headers.Add("X-API-KEY: whatevermykeyis")
> headers.Add("accept: application/json")
>
> restapi.Add("phones", "destination")
> restapi.Add("Me", "originator")
> restapi.Add(sText, "body")
> restapi.Add(phones, "phones") ' nested array of Collections
>
> hClient.URL = "https://api.myprovider.url/sms/create"
> hClient.Post("application/json", JSON.Encode(restapi), headers,
> "~/sms.log")
> End
> [/code]
>
>
So we basically can .Add a collection[] to a collection. Which never
occurred to me and is good to know, thank you.
Dmitry
More information about the User
mailing list