在Google Cloud Endpoints中如何返回动态Json

时间:2022-12-27 20:19:29

I want return a dynamic JSON object from my endpoint, to add properties on the fly without changing any method signature.

我希望从我的端点返回一个动态JSON对象,以便在不改变任何方法签名的情况下动态添加属性。

1 个解决方案

#1


1  

It feels rather contrary to the design of endpoints, but I found it very useful to have endpoints that could accept or return arbitrary JSON objects. So I use a class like this in my endpoint method:

它感觉与端点的设计相反,但我发现拥有可以接受或返回任意JSON对象的端点非常有用。所以我在我的端点方法中使用这样的类:

public class DataParcel {
  public Integer obj_type = -1;
  public List<String> json_objects = null;  // new ArrayList<String>();

The only complication is which JSON library to use - the JSON encoding/decoding is no longer done for you automatically.

唯一的复杂因素是使用哪个JSON库 - 不再为您自动完成JSON编码/解码。

#1


1  

It feels rather contrary to the design of endpoints, but I found it very useful to have endpoints that could accept or return arbitrary JSON objects. So I use a class like this in my endpoint method:

它感觉与端点的设计相反,但我发现拥有可以接受或返回任意JSON对象的端点非常有用。所以我在我的端点方法中使用这样的类:

public class DataParcel {
  public Integer obj_type = -1;
  public List<String> json_objects = null;  // new ArrayList<String>();

The only complication is which JSON library to use - the JSON encoding/decoding is no longer done for you automatically.

唯一的复杂因素是使用哪个JSON库 - 不再为您自动完成JSON编码/解码。