是否直接插入JSON

是否直接插入JSON

本文介绍了Android上的AWS DynamoDB:是否直接插入JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AWS DynamoDB存储一些灵活的文档模型数据.因为文档模型是有目的的灵活,所以我不想通过构建显式的@DynamoDBTable规范而受到约束,而是希望将JSON直接插入表中.这可能吗?我已经在信息的上下搜索.从这里出现:

I'm trying to use AWS DynamoDB to store some flexible document model data. Because the document model is purposefully flexible, I don't want to be constrained by building an explicit @DynamoDBTable specification and instead want to insert JSON directly into the table. Is this possible? I have searched low and high for this information. It appears from here:

AWS DynamoDB和Android开发(放置将项目放入DynamoDB中的表中)

答案可能不是.

推荐答案

DynamoDB确实支持灵活的JSON文档,但是有一个陷阱,您需要确保在文档中指定了Hash或Hash/Range键插入.

DynamoDB does support flexible JSON documents, but there is one catch, you will need to make sure the Hash or Hash/Range keys are specified in the document before it is inserted.

由于要避免使用带注释的Java对象,因此所需的API只是 putItem 方法.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/document/Table.html"rel =" nofollow> Table 类.此方法使您可以插入Item ,您可以使用 fromJSON 方法.

Since you want to avoid using the annotated Java objects, the API you want is simply the putItem method on the Table class. This method allows you to insert an Item, which you can construct from a JSON string using the fromJSON method.

代码如下:

DynamoDB dynamodb = new DynamoDB(client);
Table myTable = dynamodb.getTable(tableName);

// Make sure your object includes the hash or hash/range key
String myJsonString = "{\"foo\": \"bar\", \"one\": \"two\"}";

// Convert the JSON into an object
Item myItem = Item.fronJSON(myJsonString);

// Insert the Object
table.putItem(myItem);


修改

我已纠正,上面的代码段特定于AWS Java SDK,并且我错误地认为fromJSON方法可用于AWS Android SDK.

I stand corrected, the above code snippet is specific to the AWS Java SDK and I mistakenly believed the fromJSON method was available in the AWS Android SDK.

这篇关于Android上的AWS DynamoDB:是否直接插入JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:14