iOS Swift 5 API 响应不是 JSON,它有分号?

标签 ios swift api response

我正在尝试使用 api 响应,它在 postman 中很好地返回,但是当我快速打印它时,它在行尾有分号

我尝试了各种选项和各种方法来更改请求和处理响应,但均无济于事。为什么那里有分号?

******* 代码片段 ******

let todosEndpoint: String = "https://url:3000/api/v1/somestring? 
query=$filter%3DUPC%20eq%20'somenumber'"
    guard let todosURL = URL(string: todosEndpoint) else {
        print("Error: cannot create URL")
        return
    }
    var todosUrlRequest = URLRequest(url: todosURL)
todosUrlRequest.httpMethod = "GET"

todosUrlRequest.setValue("application/json", forHTTPHeaderField: 
"Content-Type")

todosUrlRequest.setValue("Bearer "+token, forHTTPHeaderField: "Authorization")


let task = URLSession.shared.dataTask(with: todosUrlRequest) { (data, response, error) in
guard let dataResponse = data,
    error == nil else {
        print(error?.localizedDescription ?? "Response Error")
    return }

    do{
        let myJson = try JSONSerialization.jsonObject(with: data!) as? NSDictionary

        print(myJson!)

******** 结果 *****

Desired Results:
{
"@odata.context": "https://api.url.com/v1/$metadata#Products",
"value": [
    {
        "ASIN": null,
        "Height": null,
        "Length": null,
        "Width": null,
        "Weight": null,
        "Cost": null,
        "Margin": null,
        "RetailPrice": null,
        "StartingPrice": null,
        "ReservePrice": null,
         }
    ]
}


Actual Results:
{
"@odata.context" = "https://api.url.com/v1/$metadata#Products";
value =     (
            {
        ASIN = "<null>";
        BlockComment = "<null>";
        BlockedDateUtc = "<null>";
        Brand = BAZZILL;
        BundleType = None;
        BuyItNowPrice = "0.99";
        CategoryCode = "<null>";
        CategoryPath = "<null>";
        Classification = "<null>";
        Condition = "<null>";
        Cost = "<null>";
        }
    );
}

最佳答案

如果您将 JSON 序列化为字典,因为您正在这样做:

let myJson = try JSONSerialization.jsonObject(with: data!) as? NSDictionary
print(myJson!)

这意味着您可以像这样访问每个字段:let comment = myJson["BlockComment"]

但是,序列化为结构可能会更好:

struct Product: Codable {
    let asin: String?
    let blockComment: String?
    let brand: String?
    let buyItNowPrice: Float?
    let cost: Float?

    enum CodingKeys: String, CodingKey {
        case asin = "ASIN"
        case blockComment = "BlockComment"
        case brand = "Brand"
        case buyItNowPrice = "BuyItNowPrice"
        case cost = "Cost"
    }
}

然后你会做:

let product = try JSONDecoder().decode(Product.self)
print(product.cost)
print(product.brand)
//etc..

https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types

https://developer.apple.com/documentation/foundation/archives_and_serialization/using_json_with_custom_types

关于iOS Swift 5 API 响应不是 JSON,它有分号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56469671/

相关文章:

java - 如何在 android 中发送带有文件上传的常规帖子键/值对

ios - FFmpeg 的音频替代品 - Core Audio IOS

objective-c - 访问 NSDictionary 时的 SIGABRT

ios - 在 Gameplaykit 中,如何在 GKAgent2D 行为中添加延迟 GKGoal 时间?

swift - 使用 SKView 更改 View ?

cocoa-touch - 您是否必须桥接到 ObjectiveC 才能快速 float ?

android - 如何在其他调用运行时以编程方式合并调用(电话 session )

java - 用 Java 创建天气应用程序

iphone - 如何从另一个类设置 UILabel 文本

ios - 为 Siesta 资源配置请求方法