【问题标题】:Swift 3 AWS DynamoDB json response deserializationSwift 3 AWS DynamoDB json 响应反序列化
【发布时间】:2017-02-09 21:45:27
【问题描述】:

努力了解如何反序列化从 AWS DynamoDB 发送的 json 对象。 我能够成功调用 dynamoDB.describeTable(describeTableInput!) 并收到详细的回复...

2017-02-08 20:30:36.054 AWS_Test[84241:3626357] AWSiOSSDK v2.5.0 [Debug] AWSURLResponseSerialization.m line:63 | -[AWSJSONResponseSerializer responseObjectForResponse:originalRequest:currentRequest:data:error:] | Response body:
{"Table":{"AttributeDefinitions":[{"AttributeName":"Author","AttributeType":"S"},{"AttributeName":"ISBN","AttributeType":"S"}],"CreationDateTime":1.486165623131E9,"GlobalSecondaryIndexes":[{"IndexArn":"arn:aws:dynamodb:ap-southeast-2:xxxxxxxxxxxxxxx:table/Books/index/ISBN-index","IndexName":"ISBN-index","IndexSizeBytes":94946,"IndexStatus":"ACTIVE","ItemCount":1780,"KeySchema":[{"AttributeName":"ISBN","KeyType":"HASH"}],"Projection":{"ProjectionType":"ALL"},"ProvisionedThroughput":{"NumberOfDecreasesToday":0,"ReadCapacityUnits":5,"WriteCapacityUnits":5}},{"IndexArn":"arn:aws:dynamodb:ap-southeast-2:xxxxxxxxxxxxxx:table/Books/index/Author-index","IndexName":"Author-index","IndexSizeBytes":94946,"IndexStatus":"ACTIVE","ItemCount":1780,"KeySchema":[{"AttributeName":"Author","KeyType":"HASH"}],"Projection":{"ProjectionType":"ALL"},"ProvisionedThroughput":{"NumberOfDecreasesToday":0,"ReadCapacityUnits":5,"WriteCapacityUnits":5}}],"ItemCount":1780,"KeySchema":[{"AttributeName":"ISBN","KeyType":"HASH"}],"ProvisionedThroughput":{"NumberOfDecreasesToday":0,"ReadCapacityUnits":5,"WriteCapacityUnits":5},"TableArn":"arn:aws:dynamodb:ap-southeast-2:xxxxxxxxxxxxxxxxx:table/Books","TableName":"Books","TableSizeBytes":94946,"TableStatus":"ACTIVE"}}

我的函数调用 Dynamo ...

func describeTable() {

        let dynamoDB = AWSDynamoDB.default()
        let describeTableInput = AWSDynamoDBDescribeTableInput()
        describeTableInput?.tableName = "Books"

        let tableDescription = dynamoDB.describeTable(describeTableInput!) as! AWSTask<AnyObject>

        let jsonResult: NSDictionary = try JSONSerialization.JSONObjectWithData(Data, options: JSONSerialization.ReadingOptions.MutableContainers) as! NSDictionary

        print(jsonResult.object(forKey: "ItemCount")!)
        // let's dump everything to see what was returned
        dump(tableDescription)

    }

但我这几天一直在苦苦挣扎,试图弄清楚如何反序列化响应并将其存储在我自己的字典中。

任何人都可以提供帮助!?谢谢!

【问题讨论】:

    标签: json amazon-web-services swift3 amazon-dynamodb


    【解决方案1】:

    您是否尝试过使用SwiftyJSON library?第三方库将使您免于头疼,尤其是在处理数据时。有了它,你的代码看起来有点像:

    let json = JSON(jsonResult)
    if let count = json["Table"]["GlobalSecondaryIndexes"]["ItemCount"].int {
      print(count) //your count should be accessible here
    }
    

    请记住,JSON 值是嵌套的,因此简单地调用 json["ItemCount"] 不会返回您所期望的。

    【讨论】:

      【解决方案2】:

      在挖掘、尖叫、挖掘和哭泣之后,我在这里找到了提示:https://github.com/awslabs/aws-sdk-ios-samples/blob/master/DynamoDBObjectMapper-Sample/Swift/DynamoDBSampleSwift/DDBDynamoDBManager.swift

      重构我的代码让我能够访问响应值..

      func describeTable() {
              let dynamoDB = AWSDynamoDB.default()
              let describeTableInput = AWSDynamoDBDescribeTableInput()
              describeTableInput?.tableName = "Books"
              let describeTask = dynamoDB.describeTable(describeTableInput!)
      
              var localTask:AWSTask<AnyObject>?
      
              localTask = describeTask.continueOnSuccessWith(block: { task -> AnyObject? in
      
                  let describeTableOutput:AWSDynamoDBDescribeTableOutput = task.result!
                  let tableName = describeTableOutput.table!.tableName
                  let tableStatus = describeTableOutput.table!.tableStatus
                  let itemCount = describeTableOutput.table?.itemCount
      
                  print("--------------------------")
                  print(tableName! as String)
                  if tableStatus == AWSDynamoDBTableStatus.active {
                      print("Active")
                  } else {
                      print("Inactive")
                  }
                  print(itemCount as! Int)
                  print("--------------------------")
      
                  return localTask
              })
          }
      

      希望这对其他人有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-30
        • 2018-08-21
        • 1970-01-01
        • 1970-01-01
        • 2016-10-17
        • 2021-05-14
        • 1970-01-01
        相关资源
        最近更新 更多