Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Swift 2 Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Swift 2) Modify Parts of JSON Document

Demonstrates how to modify parts of a JSON document. This example uses the following JSON document:

{
   "fruit": [
      	{
         "kind": "apple",
	 "count": 24,
	 "fresh": true,
	 "extraInfo": null,
	 "listA": [ "abc", 1, null, false ],
	 "objectB": { "animal" : "monkey" }
      	},
	{
         "kind": "pear",
	 "count": 18,
	 "fresh": false,
	 "extraInfo": null
	 "listA": [ "xyz", 24, null, true ],
	 "objectB": { "animal" : "lemur" }
	}
    ],
    "list" : [ "banana", 12, true, null, "orange", 12.5, { "ticker": "AAPL" }, [ 1, 2, 3, 4, 5 ] ],
    "alien" : true
}

Chilkat Downloads for the Swift Programming Language

MAC OS X (Cocoa) Objective-C/Swift Libs

iOS Objective-C/Swift Libs

func chilkatTest() {
    let json = CkoJsonObject()

    // Load the JSON from a file.
    var success: Bool = json.LoadFile("qa_data/json/modifySample.json")
    if success != true {
        print("\(json.LastErrorText)")
        return
    }

    // This example will not check for errors (i.e. null / false / 0 return values)...

    // Get the "list" array:
    var listA: CkoJsonArray? = json.ArrayOf("list")

    // Modify values in the list.

    // Change banana to plantain
    success = listA!.SetStringAt(0, value: "plantain")

    // Change 12 to 24
    success = listA!.SetIntAt(1, value: 24)

    // Change true to false
    success = listA!.SetBoolAt(2, value: false)

    // Is the 3rd item null?
    var bNull: Bool = listA!.IsNullAt(3)

    // Change "orange" to 32.
    success = listA!.SetIntAt(4, value: 32)

    // Change 12.5 to 31.2
    success = listA!.SetNumberAt(5, value: "31.2")

    // Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" }
    // Do this by deleting, then inserting a new object at the same location.
    success = listA!.DeleteAt(6)
    success = listA!.AddObjectAt(6)
    var tickerObj: CkoJsonObject? = listA!.ObjectAt(6)
    success = tickerObj!.AddStringAt(-1, name: "ticker", value: "GOOG")

    tickerObj = nil

    // Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
    success = listA!.DeleteAt(7)
    success = listA!.AddArrayAt(7)
    var aa: CkoJsonArray? = listA!.ArrayAt(7)
    success = aa!.AddStringAt(-1, value: "apple")
    success = aa!.AddIntAt(-1, value: 22)
    success = aa!.AddBoolAt(-1, value: true)
    success = aa!.AddNullAt(-1)
    success = aa!.AddNumberAt(-1, numericStr: "1080.25")
    aa = nil

    listA = nil

    // Get the "fruit" array
    var aFruit: CkoJsonArray? = json.ArrayAt(0)

    // Get the 1st element:
    var appleObj: CkoJsonObject? = aFruit!.ObjectAt(0)

    // Modify values by member name:
    success = appleObj!.SetStringOf("fruit", value: "fuji_apple")
    success = appleObj!.SetIntOf("count", value: 46)
    success = appleObj!.SetBoolOf("fresh", value: false)
    success = appleObj!.SetStringOf("extraInfo", value: "developed by growers at the Tohoku Research Station in Fujisaki")

    appleObj = nil

    // Modify values by index:
    var pearObj: CkoJsonObject? = aFruit!.ObjectAt(1)
    success = pearObj!.SetStringAt(0, value: "bartlett_pear")
    success = pearObj!.SetIntAt(1, value: 12)
    success = pearObj!.SetBoolAt(2, value: false)
    success = pearObj!.SetStringAt(3, value: "harvested in late August to early September")
    pearObj = nil

    aFruit = nil

    json.EmitCompact = false
    print("\(json.Emit())")

}

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.