Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Swift 3,4,5...) Modify Parts of JSON DocumentDemonstrates 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 }
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.array(of: "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!.isNull(at: 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!.delete(at: 6) success = listA!.addObject(at: 6) var tickerObj: CkoJsonObject? = listA!.object(at: 6) success = tickerObj!.addString(at: -1, name: "ticker", value: "GOOG") tickerObj = nil // Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]" success = listA!.delete(at: 7) success = listA!.add(at: 7) var aa: CkoJsonArray? = listA!.array(at: 7) success = aa!.addString(at: -1, value: "apple") success = aa!.addInt(at: -1, value: 22) success = aa!.addBool(at: -1, value: true) success = aa!.addNull(at: -1) success = aa!.addNumber(at: -1, numericStr: "1080.25") aa = nil listA = nil // Get the "fruit" array var aFruit: CkoJsonArray? = json.array(at: 0) // Get the 1st element: var appleObj: CkoJsonObject? = aFruit!.object(at: 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!.object(at: 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.