Skip to content

harrisonv789/JsonKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JsonKit

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

Swift Package Index


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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages