JsonKit

0.1.0

A Swift library that makes reading and writing JSON data objects in Swift and converting to the appropriate Swift data objects easy.
harrisonv789/JsonKit

What's New

0.1.0

2024-02-17T08:10:55Z

First version of the JSON Kit functionality, with some basic functions for reading and writing JSON data in Swift.

JsonKit

A Swift library that makes reading and writing JSON data objects in Swift and converting to the appropriate Swift data objects easy.


Reading Data

To read a value from a JSON string, use the get function:

let jsonString = "{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}"

if let age = get(from: jsonString, key: "age", defaultValue: 10) as? Int {
    print("Age: \(age)")
}

This function contains an optional defaultValue parameter. This parameter will be returned if there is an issue with the JSON data or if the value does not exist.


Writing Data

To write data in a JSON string, use the set function:

let jsonString = "{\"name\": \"John\", \"age\": 30, \"city\": \"New York\"}"

if let updatedJsonString = set(in: jsonString, forKey: "age", value: 31) {
    print("Updated JSON String: \(updatedJsonString)")
}

If there is an error with the JSON data, then a nil value will be returned. Otherwise, the new JSON string with the updated data will be returned.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Apr 15 2024 16:12:34 GMT-0900 (Hawaii-Aleutian Daylight Time)