【问题标题】:Parsing JSON answer from an AWS Lambda function从 AWS Lambda 函数解析 JSON 答案
【发布时间】:2019-06-08 11:53:45
【问题描述】:

我刚刚让 AWS Lambda 函数按预期工作,但我无法解析它的 JSON 答案。 我为此使用 SwiftyJSON。

这是我的测试代码:

let json = JSON(task.result!)
print("SWyJSON: \(json)")

if let jsonDic = json.dictionary {
    print("SWyJSON2a: \(jsonDic)")
    print("SWyJSON2b: \(String(describing: jsonDic["body"]!))")
    if let x = json.dictionary?["body"]?.dictionary {
        print("SWyJSON2c: \(String(describing: x["Users"]))")
    }
}

SWyJSON: {
  "inBound" : "9bf69.....14d5ac4",
  "body" : "{\"Users\":[{\"Username\":\"test1\",\"Attributes\":[{\"Name\":\"email\",    \"Value\":\"y547170@nidtv.net\"}],\"UserCreateDate\":\"2019-06-03T02:53:03.300Z\",  \"UserLastModifiedDate\":\"2019-06-03T02:53:56.580Z\",\"Enabled\":true,\"UserStatus\":\"CONFIRMED\"}]}",
  "statusCode" : 200
}

SWyJSON2a: ["inBound": 9bf69.....14d5ac4, "body": {"Users":[{"Username":"test1","Attributes":[  {"Name":"email","Value":"y547170@nidtv.net"}],"UserCreateDate":"2019-06-03T02:53:03.300Z",    "UserLastModifiedDate":"2019-06-03T02:53:56.580Z","Enabled":true,"UserStatus":"CONFIRMED"}]}, "statusCode":     200]

SWyJSON2b: {"Users":[{"Username":"test1","Attributes":[{"Name":"email","Value":"y549860@nwytg.net"}],   "UserCreateDate":"2019-06-03T02:53:03.300Z","UserLastModifiedDate":"2019-06-03T02:53:56.580Z", "Enabled":true,"UserStatus":"CONFIRMED"}]}

我的问题是:代码有什么问题导致最后一次打印没有给出任何结果?

SWyJSON、SWyJSON2a 和 SWyJSON2b 在 Xcode 调试控制台中显示了一些结果,如我所料,但不是 SWyJSON2c。我期望的地方是:

"Users":[{"Username":"test1","Attributes":[{"Name":"email","Value":"y549860@nwytg.net"}],   "UserCreateDate":"2019-06-03T02:53:03.300Z","UserLastModifiedDate":"2019-06-03T02:53:56.580Z", "Enabled":true,"UserStatus":"CONFIRMED"}]

或:

[{"Username":"test1","Attributes":[{"Name":"email","Value":"y549860@nwytg.net"}],   "UserCreateDate":"2019-06-03T02:53:03.300Z","UserLastModifiedDate":"2019-06-03T02:53:56.580Z", "Enabled":true,"UserStatus":"CONFIRMED"}]

【问题讨论】:

    标签: json swift amazon-web-services aws-lambda swifty-json


    【解决方案1】:

    body 值是字符串而不是字典,所以更改

    if let x = json.dictionary?["body"]?.dictionary {
    

    if let x = json.dictionary?["body"]?.string {
    
        let data = Data(x.utf8)
    
        let content = try? JSONSerialization.jsonObject(with:data, options: [])
    
        if let _  =  content as? [[String:Any]] { // array 
    
           let res = try?  JSONDecoder().decode([Root].self,from:data)
    
           print(res)
    
        }
        else if let dic  =  content as? [String:Any] {  {
    
           guard let users = dic["Users"] else { return } // dictionay
    
           guard let jsonData = try? JSONSerialization.data(withJSONObject: users, options:[]) else { return }
    
           let res = try?  JSONDecoder().decode([Root].self,from:jsonData)
    
           print(res)
    
        }
    }
    
    
      // MARK: - Element
    struct Root: Codable {
        let username: String
        let attributes: [Attribute]
        let userCreateDate, userLastModifiedDate: String
        let enabled: Bool
        let userStatus: String
    
        enum CodingKeys: String, CodingKey {
            case username = "Username"
            case attributes = "Attributes"
            case userCreateDate = "UserCreateDate"
            case userLastModifiedDate = "UserLastModifiedDate"
            case enabled = "Enabled"
            case userStatus = "UserStatus"
        }
    }
    
    // MARK: - Attribute
    struct Attribute: Codable {
        let name, value: String
    
        enum CodingKeys: String, CodingKey {
            case name = "Name"
            case value = "Value"
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 2022-11-23
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      相关资源
      最近更新 更多