SwiftyJSON is the leader in handling JSON data in Swift

SwiftyJSON is the leader in handling JSON data in Swift

2022-10-31 0 557
Resource Number 47229 Last Updated 2025-02-21
¥ 0USD 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/kyym/swiftyjson-is-the-leader-in-handling-json-data-in-swift.html

Share free open-source source code

Q&A
  • 1, automatic: after taking the photo, click the (download) link to download; 2. Manual: After taking the photo, contact the seller to issue it or contact the official to find the developer to ship.
View details
  • 1, the default transaction cycle of the source code: manual delivery of goods for 1-3 days, and the user payment amount will enter the platform guarantee until the completion of the transaction or 3-7 days can be issued, in case of disputes indefinitely extend the collection amount until the dispute is resolved or refunded!
View details
  • 1. Heptalon will permanently archive the process of trading between the two parties and the snapshots of the traded goods to ensure that the transaction is true, effective and safe! 2, Seven PAWS can not guarantee such as "permanent package update", "permanent technical support" and other similar transactions after the merchant commitment, please identify the buyer; 3, in the source code at the same time there is a website demonstration and picture demonstration, and the site is inconsistent with the diagram, the default according to the diagram as the dispute evaluation basis (except for special statements or agreement); 4, in the absence of "no legitimate basis for refund", the commodity written "once sold, no support for refund" and other similar statements, shall be deemed invalid; 5, before the shooting, the transaction content agreed by the two parties on QQ can also be the basis for dispute judgment (agreement and description of the conflict, the agreement shall prevail); 6, because the chat record can be used as the basis for dispute judgment, so when the two sides contact, only communicate with the other party on the QQ and mobile phone number left on the systemhere, in case the other party does not recognize self-commitment. 7, although the probability of disputes is very small, but be sure to retain such important information as chat records, mobile phone messages, etc., in case of disputes, it is convenient for seven PAWS to intervene in rapid processing.
View details
  • 1. As a third-party intermediary platform, Qichou protects the security of the transaction and the rights and interests of both buyers and sellers according to the transaction contract (commodity description, content agreed before the transaction); 2, non-platform online trading projects, any consequences have nothing to do with mutual site; No matter the seller for any reason to require offline transactions, please contact the management report.
View details

Related Article

make a comment
No comments available at the moment
Official customer service team

To solve your worries - 24 hours online professional service