简体   繁体   English

在Java中将AWS APIGateway的模型转换为JSON

[英]Convert AWS APIGateway's Model to JSON in Java

I am using Amazon's APIGateway service client side. 我正在使用Amazon的APIGateway服务客户端。 When you make a request the data returned is stored in a Model data type that the schema is set up beforehand. 当您发出请求时,返回的数据存储在Model数据类型中,该数据类型是预先设置的。 the calls look like this: 呼叫看起来像这样:

MyModel myModel = client.settingsPost();
String volume = myModel.getVolume();

the schema for this simple object would look like this: 这个简单对象的架构如下所示:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "MyModel",
  "type": "object",
  "properties" : {
      "volume" : { "type" : "string" }
  }
}

I would like to convert the Model returned directly to JSON instead of having to go manually reconstruct a new JSONObject from each value of this Model. 我想直接将返回的Model转换为JSON,而不必从该Model的每个值手动重建一个新的JSONObject。 The Models seem to be very simple and I cant even iterate through them. 模型似乎非常简单,我什至无法遍历它们。 But I wonder if there is a way to convert them using the GSON library somehow? 但是我想知道是否有某种方法可以使用GSON库进行转换?

EDIT: I am using the APIGateway SDK generated to Java. 编辑:我正在使用Java生成的APIGateway SDK。

Using Jackson: 使用杰克逊:

ObjectMapper mapper = new ObjectMapper();
String jsonInString = mapper.writeValueAsString(myModel);

Using Gson: 使用Gson:

Gson gson = new Gson();
String json = gson.toJson(myModel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM