SwiftyJSON is the leader in handling JSON data in Swift

SwiftyJSON is the leader in handling JSON data in Swift

2022-10-31 0 881
Resource Number 47229 Last Updated 2025-02-21
¥ 0HKD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation

The SwiftyJSON recommended in this issue makes it easy to work with JSON data in Swift.

SwiftyJSON is the leader in handling JSON data in Swift插图

Why is the typical JSON in Swift not handled well?

Swift is very strict about types. But while explicit typing helps us avoid errors, it can become painful when dealing with JSON and other domains that are inherently implicit in typing.
Take the Twitter API as an example. Suppose we want to retrieve the “name” value of some of a user’s tweets in Swift (according to Twitter’s API).

The code is as follows:

if let statusesArray = try?  JSONSerialization.jsonObject(with: data, options:  .allowFragments) as?  [[String: Any]],
let user = statusesArray[0]["user"] as?  [String: Any],
let username = user["name"] as?  String {
// Finally we got the username
}

Even if we used an optional chain, it would be messy:

if let JSONObject = try JSONSerialization.jsonObject(with: data, options:  .allowFragments) as?  [[String: Any]],
let username = (JSONObject[0]["user"] as?  [String: Any])? ["name"] as?  String {
// There's our username
}

Looks very messy for something that should be simple.

With SwiftyJSON, all you have to do is:

let json = JSON(data: dataFromNetworking)
if let userName = json[0]["user"]["name"].string {
//Now you got your value
}

Don’t worry about the optional wrapping thing, it will be done for you automatically.

let json = JSON(data: dataFromNetworking)
let result = json[999999]["wrong_key"]["wrong_name"]
if let userName = result.string {
    //Calm down, take it easy, the ".string" property still produces the correct Optional String type with safety
} else {
    //Print the error
    print(result.error)
}

Tool requirements

  • iOS 8.0+ | macOS 10.10+ | tvOS 9.0+ | watchOS 2.0+
  • Xcode 8

Use

CocoaPods (iOS 8+, OS X 10.9+)

You can install it using CocoaPods by adding it to your Podfile:

platform :ios,  '8.0'
use_frameworks!

target 'MyApp' do
pod 'SwiftyJSON', '~>  4.0 '< / span >
end

Carthage (iOS 8+, OS X 10.9+)

You can do this by adding CarthageSwiftyJSON to your Cartfile:

github "SwiftyJSON/SwiftyJSON" ~>  4.0

If you are building dependencies using Carthage, make sure you have added SwiftyJSON.framework to the “Linking Frameworks and libraries” section of the target and included them in your Carthage Framework copy build phase.

Swift Package Manager

You can install using the Swift package Manager by adding the correct description to the file: SwiftyJSONPackage.swift

// swift-tools-version:4.0
import PackageDescription

let package = Package(
name: "YOUR_PROJECT_NAME",
dependencies: [
.package(url:  "https://github.com/SwiftyJSON/SwiftyJSON.git", from:  "4.0.0"),
]
)

Then swift build when you are ready.

Usage

Initialization

import SwiftyJSON
let json = JSON(data:  dataFromNetworking)

or

let json = JSON(jsonObject)

or

if let dataFromString = jsonString.data(using: .utf8, allowLossyConversion: false) {
    let json = JSON(data: dataFromString)
}

Subscript

// Getting a double from a JSON Array
let name = json[0].double
// Getting an array of string from a JSON Array
let arrayNames =  json["users"].arrayValue.map {$0["name"].stringValue}
// Getting a string from a JSON Dictionary
let name = json["name"].stringValue
// Getting a string using a path to the element
let path: [JSONSubscriptType] = [1,"list",2,"name"]
let name = json[path].string
// Just the same
let name = json[1]["list"][2]["name"].string
// Alternatively
let name = json[1,"list",2,"name"].string
// With a hard way
let name = json[].string
// With a custom way
let keys:[JSONSubscriptType] = [1,"list",2,"name"]
let name = json[keys].string

Error

SwiftyJSON 4.x

SwiftyJSON 4.x introduces an enumeration type called SwiftyJSONError, These include unsupportedType, indexOutOfBounds, elementTooDeep, wrongType,notExist and invalidJSON, while ErrorDomain is
SwiftyJSONError. ErrorDomain. Note: these old error types in SwiftyJSON 4 x has been deprecated, and will be deleted in future versions.

SwiftyJSON 3.x

Use subscripts to get/set values in an array or dictionary

If JSON is:

  • An array, the application may crash due to “index out of bounds”.
  • A dictionary that will be assigned to it by nil for no reason.
  • is not an array or dictionary and the application may crash due to an “unrecognized selector” exception.

This will never happen in SwiftyJSON.

let json = JSON(["name", "age"])
if let name = json[999].string {
    // Do something you want
} else {
    print(json[999].error!) // "Array[999] is out of bounds"
}
let json = JSON(["name":"Jack", "age": 25])
if let name = json["address"].string {
    // Do something you want
} else {
    print(json["address"].error!) // "Dictionary["address"] does not exist"
}
let json = JSON(12345)
if let age = json[0].string {
    // Do something you want
} else {
    print(json[0])       // "Array[0] failure, It is not an array"
    print(json[0].error!) // "Array[0] failure, It is not an array"
}

if let name = json["name"].string {
    // Do something you want
} else {
    print(json["name"])       // "Dictionary[\"name"] failure, It is not an dictionary"
    print(json["name"].error!) // "Dictionary[\"name"] failure, It is not an dictionary"
}

Merge

You can merge one JSON into another. Merging one JSON into another adds all the values that don’t exist to the original JSON that only exists in otherJSON.

If two JSons contain values for the same key, the value is usually overwritten in the original JSON, but in both cases it provides some special handling:

  • If both values are the values of an array found by json.type.array in otherJSON, they are appended to the array values of the original JSON.
  • If both values are one, then json.type.dictionary The two JSON values are merged in the same way that encapsulating JSON is merged.

In cases where two fields in JSON have different types, the value will always be overwritten.

There are two different ways to merge: merge modifies the original JSON, and merged works nondestructively on a copy.

let original: JSON = [
    "first_name": "John",
    "age": 20,
    "skills": ["Coding", "Reading"],
    "address": [
        "street": "Front St",
        "zip": "12345",
    ]
]

let update: JSON = [
    "last_name": "Doe",
    "age": 21,
    "skills": ["Writing"],
    "address": [
        "zip": "12342",
        "city": "New York City"
    ]
]

let updated = original.merge(with: update)
// [
//     "first_name": "John",
//     "last_name": "Doe",
//     "age": 21,
//     "skills": ["Coding", "Reading", "Writing"],
//     "address": [
//         "street": "Front St",
//         "zip": "12342",
//         "city": "New York City"
//     ]
// ]

The string indicates

.

There are two options:

  • Use one of the default Swift
  • Use the custom option “null” that handles options well and represents nil as:
let dict = ["1":2, "2":"two",  "3": nil] as [String: Any?]
let json = JSON(dict)
let representation = json.rawString(options: [.castNilToNSNull:  true])
// representation is "{\"1\":2,\"2\":\"two\",\"3\":null}",  which represents {"1":2,"2":"two","3":null}

—END—

Open Source license: MIT license

资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free Source Code SwiftyJSON is the leader in handling JSON data in Swift https://ictcoder.com/swiftyjson-is-the-leader-in-handling-json-data-in-swift/

Share free open-source source code

Q&A
  • 1. Automatic: After making an online payment, click the (Download) link to download the source code; 2. Manual: Contact the seller or the official to check if the template is consistent. Then, place an order and make payment online. The seller ships the goods, and both parties inspect and confirm that there are no issues. ICTcoder will then settle the payment for the seller. Note: Please ensure to place your order and make payment through ICTcoder. If you do not place your order and make payment through ICTcoder, and the seller sends fake source code or encounters any issues, ICTcoder will not assist in resolving them, nor can we guarantee your funds!
View details
  • 1. Default transaction cycle for source code: The seller manually ships the goods within 1-3 days. The amount paid by the user will be held in escrow by ICTcoder until 7 days after the transaction is completed and both parties confirm that there are no issues. ICTcoder will then settle with the seller. In case of any disputes, ICTcoder will have staff to assist in handling until the dispute is resolved or a refund is made! If the buyer places an order and makes payment not through ICTcoder, any issues and disputes have nothing to do with ICTcoder, and ICTcoder will not be responsible for any liabilities!
View details
  • 1. ICTcoder will permanently archive the transaction process between both parties and snapshots of the traded goods to ensure the authenticity, validity, and security of the transaction! 2. ICTcoder cannot guarantee services such as "permanent package updates" and "permanent technical support" after the merchant's commitment. Buyers are advised to identify these services on their own. If necessary, they can contact ICTcoder for assistance; 3. When both website demonstration and image demonstration exist in the source code, and the text descriptions of the website and images are inconsistent, the text description of the image shall prevail as the basis for dispute resolution (excluding special statements or agreements); 4. If there is no statement such as "no legal basis for refund" or similar content, any indication on the product that "once sold, no refunds will be supported" or other similar declarations shall be deemed invalid; 5. Before the buyer places an order and makes payment, the transaction details agreed upon by both parties via WhatsApp or email can also serve as the basis for dispute resolution (in case of any inconsistency between the agreement and the description of the conflict, the agreement shall prevail); 6. Since chat records and email records can serve as the basis for dispute resolution, both parties should only communicate with each other through the contact information left on the system when contacting each other, in order to prevent the other party from denying their own commitments. 7. Although the probability of disputes is low, it is essential to retain important information such as chat records, text messages, and email records, in case a dispute arises, so that ICTcoder can intervene quickly.
View details
  • 1. As a third-party intermediary platform, ICTcoder solely protects transaction security and the rights and interests of both buyers and sellers based on the transaction contract (product description, agreed content before the transaction); 2. For online trading projects not on the ICTcoder platform, any consequences are unrelated to this platform; regardless of the reason why the seller requests an offline transaction, please contact the administrator to report.
View details

Related Source code

ICTcoder Customer Service

24-hour online professional services