gpt4 book ai didi

json - 如何获取json字段?

转载 作者:行者123 更新时间:2023-11-28 05:38:40 28 4
gpt4 key购买 nike

我听了一门类(class)的课我需要获取 json,但我想获取另一个 json,而不是在类(class)中。

所以这是我的 json: https://api.scryfall.com/cards/search?q=half

和代码:

struct Card {
var cardId: String
var name: String
var imageUrl: String
var text: String

init?(dict: [String: AnyObject]){
guard let name = dict["name"] as? String,
let cardId = dict["cardId"] as? String,
let imageUrl = dict["imageUrl"] as? String,
let text = dict["text"] as? String else { return nil }

self.cardId = cardId
self.name = name
self.imageUrl = imageUrl
self.text = text
}
}

class CardNetworkService{
private init() {}

static func getCards(url: String, completion: @escaping(GetCardResponse) -> ()) {
guard let url = URL(string: url) else { return }

NetworkService.shared.getData(url: url) { (json) in
do {
print ("ok1")
let response = try GetCardResponse(json: json)
print ("ok2")
completion(response)
} catch {
print(error)
}
}
}
}

class NetworkService {

private init() {}

static let shared = NetworkService()

func getData(url: URL, completion: @escaping (Any) -> ()) {
let session = URLSession.shared

session.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }

do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
DispatchQueue.main.async {
completion(json)
}
print(json)
} catch {
print(error)
}
}.resume()
}
}


struct GetCardResponse{
let cards: [Card]

init(json: Any) throws {
guard let array = json as? [[String: AnyObject]] else { throw NetworkError.failInternetError }

var cards = [Card]()
for dictionary in array {
guard let card = Card(dict: dictionary) else { continue }
cards.append(card)
}
self.cards = cards
}
}

struct GetCardResponse[[String: AnyObject]] 中的问题,因为我不知道如何解析这种类型的 json。我试图以 json 的方式更改它们。但我真的不明白它是如何工作的以及我需要在代码的哪一部分中放置 json["data"] 或类似的东西......请帮助。我只想获取 json 字段 tcgplayer_id, name, art_crop

最佳答案

根据您的代码,您可以将所需的详细信息解析为:

struct Card {
var cardId: String = ""
var name: String = ""
var imageUrl: String = ""
var text: String = ""

init(dict: [String: Any]) {

if let obj = dict["name"] {
self.name = "\(obj)"
}

if let obj = dict["tcgplayer_id"] {
self.cardId = "\(obj)"
}

if let obj = dict["image_uris"] as? [String:Any], let url = obj["art_crop"] {
self.imageUrl = "\(url)"
}

if let obj = dict["oracle_text"] {
self.text = "\(obj)"
}
}

static func models(array: [[String:Any]]) -> [Card] {
return array.map { Card(dict: $0) }
}
}

class CardNetworkService{
private init() {}

static func getCards(url: String, completion: @escaping([Card]?) -> ()) {
guard let url = URL(string: url) else { return }

NetworkService.shared.getData(url: url) { (json) in
print ("ok1")
if let jData = json as? [String:Any], let data = jData["data"] as? [[String:Any]] {
let response = Card.models(array: data)
completion(response)
}
completion(nil)
}
}
}

class NetworkService {

private init() {}

static let shared = NetworkService()

func getData(url: URL, completion: @escaping (Any) -> ()) {
let session = URLSession.shared

session.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }

do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
DispatchQueue.main.async {
completion(json)
}
} catch {
print(error)
}
}.resume()
}
}

CardNetworkService.getCards(url: "https://api.scryfall.com/cards/search?q=half") { (res) in
print(res ?? [])
}

只需将此代码粘贴到 Playground 上即可。

快乐编码:)

关于json - 如何获取json字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57727751/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com