解析DynamoDB batchGetItem JSON输出

时间:2018-05-09 04:52:42

标签: ios json swift amazon-web-services amazon-dynamodb

我有一个Swift函数,可以在AWS DynamoDB中执行batchGetItem。输出以JSON格式返回,但是我很难解析它。下面是我尝试用来解析数据的代码,以及JSON输出的样子。任何帮助深表感谢。感谢。

功能:

func batchGetItem () {

        let DynamoDB = AWSDynamoDB.default()

        var keys = [Any]()

        let idNumbers = Array
        let n = 0

        for i in 0...n {

            let value = AWSDynamoDBAttributeValue()
            value?.n = String(idNumbers[i])
            keys.append(["numID": value])

        }
let keysAndAttributesMap = AWSDynamoDBKeysAndAttributes()
        keysAndAttributesMap?.keys = keys as? [[String : AWSDynamoDBAttributeValue]]
        keysAndAttributesMap?.consistentRead = true
        let tableMap = ["tableName" : keysAndAttributesMap]
        let request = AWSDynamoDBBatchGetItemInput()
        request?.requestItems = tableMap as? [String : AWSDynamoDBKeysAndAttributes]
        request?.returnConsumedCapacity = AWSDynamoDBReturnConsumedCapacity.total
        DynamoDB.batchGetItem(request!) { (output, error) in

            if output != nil {
                print("Batch Query output?.responses?.count:", output!.responses!)
                print(output!.responses!)

                do {
                    let json = try JSONSerialization.jsonObject(with: output, options: .allowFragments) as! [String:Any]
                    let posts = json["posts"] as? [[String: Any]] ?? []
                    print(posts)
                } catch let error as NSError {
                    print(error)
                }
            }
            if error != nil {
                print("Batch Query error:", error!)
            }
        }
    }

Xcode错误消息 Ambiguous reference to member 'jsonObject(with:options:)'

JSON输出

["tableName": 
    [
        [
            "hashKey":  < AWSDynamoDBAttributeValue: 0x1c00b3e60 > {
                N = 1;
            }, 
            "foo":  < AWSDynamoDBAttributeValue: 0x1c40b4b20 > {
                S = dog;
            }, 
            "numID":  < AWSDynamoDBAttributeValue: 0x1c00b3e00 > {
                N = 1;
            }, 
            "name":  < AWSDynamoDBAttributeValue: 0x1c80b55a0 > {
                S = "John Doe";
            }, 
            "link":  < AWSDynamoDBAttributeValue: 0x1c40b4b80 > {
                S = "http://";
            }, 
            "idNum":  < AWSDynamoDBAttributeValue: 0x1c80b5660 > {
                N = 67343;
            }
        ]
    ]
]

1 个答案:

答案 0 :(得分:0)

您可以使用DynamoDB Object Mapper为您解析数据。 https://docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-nosql-database.html#add-aws-mobile-nosql-database-crud

数据类的示例设置如下:

import Foundation
import UIKit
import AWSDynamoDB

class Post: AWSDynamoDBObjectModel, AWSDynamoDBModeling {

    @objc var foo: String?
    @objc var numID: String?
    @objc var name: String?
    @objc var link: String?
    @objc var idNum: String?

    class func dynamoDBTableName() -> String {

        return "MY_TABLE"
    }

    class func hashKeyAttribute() -> String {

        return "numId"
    }

}