2017-02-09 14 views
0

AWS DynamoDBから送信されたjsonオブジェクトを逆シリアル化する方法を理解するために苦労しています。 私は成功しdynamoDB.describeTable(describeTableInputを!)を呼び出し、詳細な応答を受信することができるよ...Swift 3 AWS DynamoDB json応答の逆シリアル化

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

My機能

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) 

    } 

ダイナモ...

を呼び出すしかし、私は苦労してきました応答を逆シリアル化して自分自身の辞書に格納する方法を試しています。

誰でも手伝うことができます!どうも!

答えて

1

SwiftyJSON libraryを試しましたか?第三者の図書館は、特にデータを扱う際に大規模な頭痛からあなたを救うでしょう。これで、あなたのコードは少しのようになります。

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

はJSON値がネストされているので、単に[「ITEMCOUNT個」] JSONを呼び出すと、あなたが期待しているものを返さないことに注意してください。

0

掘りと叫んと開削泣いた後、私はここにヒントが見つかりました:私のコードをリファクタリング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 
     }) 
    } 

・ホープこれは誰かに役立ちます!