简体   繁体   English

根据JSON响应创建对象,修改值,然后在Swift中返回

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

In an app I recently developed, the client wanted to be able to create a NEW user in their POS system. 在我最近开发的应用程序中,客户希望能够在其POS系统中创建一个NEW用户。 Their POS system is web-based and has a massive API, so this was easy. 他们的POS系统是基于Web的,并且具有大量的API,因此这很容易。

Now they want the app to see if the user exists before creating a duplicate user, which is fine. 现在他们希望应用程序在创建重复用户之前先查看用户是否存在,这很好。 However, they also want to update the information in the user record if it is a duplicate. 但是,如果重复,他们还希望更新用户记录中的信息。

The POS system's API returns an extensive "Customer" JSON response with 250+ fields. POS系统的API返回包含250多个字段的广泛的“客户” JSON响应。 I only need to update 4 of those fields. 我只需要更新其中四个字段。

Is there a way to easily create an object from the "Get Customer" response (it is a multi-dimensional array), edit specific values, and then post that object back as JSON to the "Update Customer" method? 有没有一种方法可以通过“获取客户”响应(它是一个多维数组)轻松创建对象,编辑特定值,然后将该对象以JSON形式发布回“更新客户”方法?

Edit #1 编辑#1

Still having problems wrapping my head around this. 仍然无法解决这个问题。 To further clarify the process and how it works: 为了进一步阐明该过程及其工作方式:

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.

Here's what I've done so far: 到目前为止,这是我所做的:

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
}

This works to make the dictionary look similar to the JSON, however I am concerned about the multi-dimensional aspect of the Customer JSON. 这可以使字典看起来类似于JSON,但是我担心Customer JSON的多维方面。 I am not sure that value , once checked for being a dictionaryObject will be keep it's K,V relationship. 我不确定value一旦检查是否为dictionaryObject会保持其K,V关系。 I haven't tested yet though. 我还没有测试。

Once I get the existingCustomer Dictionary complete, I can then iterate through the entries to create the params to POST/PUT, however, again, they need to retain their KV relationship. 一旦完成existingCustomer Customer Dictionary,就可以遍历条目以创建POST / PUT的参数,但是,再次,它们需要保留其KV关系。

The params generally look like: 这些参数通常如下所示:

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"
]

Only much, much longer. 只有很多,更长。 There's something like 200 KVs 大概有200 KV

Using @GoodByeStackOverflow's Aldwych repository, I was able to get this solved fairly easily. 使用@GoodByeStackOverflow的Aldwych存储库,我能够很轻松地解决此问题。 I was able to directly modify a cloned version of the JSON object and then send it back to the API server as an NSDictionary. 我能够直接修改JSON对象的克隆版本,然后将其作为NSDictionary发送回API服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM