簡體   English   中英

根據JSON響應創建對象,修改值,然后在Swift中返回

[英]Create an object from JSON response, modify values, and return it in Swift

在我最近開發的應用程序中,客戶希望能夠在其POS系統中創建一個NEW用戶。 他們的POS系統是基於Web的,並且具有大量的API,因此這很容易。

現在他們希望應用程序在創建重復用戶之前先查看用戶是否存在,這很好。 但是,如果重復,他們還希望更新用戶記錄中的信息。

POS系統的API返回包含250多個字段的廣泛的“客戶” JSON響應。 我只需要更新其中四個字段。

有沒有一種方法可以通過“獲取客戶”響應(它是一個多維數組)輕松創建對象,編輯特定值,然后將該對象以JSON形式發布回“更新客戶”方法?

編輯#1

仍然無法解決這個問題。 為了進一步闡明該過程及其工作方式:

1) API call to get user information 2) User information returned via JSON. The response is a REALLY big, multi-dimensional, response. 3) 4 of the fields in the returned JSON customer have to be edited 4) JSON then needs to be used to create "Parameters" to PUT/POST back to the API.

到目前為止,這是我所做的:

var existingCustomer = NSMutableDictionary()

...Function to acquire JSON using Alamofire...

var json = JSON(response!)
let d = json["Customer"]["Customer"].dictionaryValue

for (k, v) in d {
    if let value = v.string {
        self.existingCustomer[k] = value
    }
    //CHECK FOR OTHER TYPES
}

這可以使字典看起來類似於JSON,但是我擔心Customer JSON的多維方面。 我不確定value一旦檢查是否為dictionaryObject會保持其K,V關系。 我還沒有測試。

一旦完成existingCustomer Customer Dictionary,就可以遍歷條目以創建POST / PUT的參數,但是,再次,它們需要保留其KV關系。

這些參數通常如下所示:

var params = [
    "firstName":"John",
    "lastName":"Appleseed",
    "photos": [
        "url":"www.website.com",
        "width":1024,
        "height":768
    ],
    "addresses": [
        "shippingAddress": [
            "street":"123 Test Road"
        ],
        "homeAddress": [
            "street":"456 Test Crescent"
        ]
    ],
    "phone":"555-555-5555"
]

只有很多,更長。 大概有200 KV

使用@GoodByeStackOverflow的Aldwych存儲庫,我能夠很輕松地解決此問題。 我能夠直接修改JSON對象的克隆版本,然后將其作為NSDictionary發送回API服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM