正在解析来自 AWS Lambda 函数的 JSON 答案
Parsing JSON answer from an AWS Lambda function
我刚刚让 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 调试控制台中显示了一些结果,但 SWy[=25 没有=]2c。我期望的地方是:
"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"}]
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"
}
}
我刚刚让 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 调试控制台中显示了一些结果,但 SWy[=25 没有=]2c。我期望的地方是:
"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"}]
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"
}
}