【问题标题】:Concatenate JsonRepresentation连接 JsonRepresentation
【发布时间】:2010-01-10 06:16:51
【问题描述】:

如何在不构建自己的字符串解析器的情况下将多个 JsonRepresentation 对象连接为一个?

假设我有两个 JsonRepresentation 对象

obj1 = {"name":"obj1"};
obj2 = {"name":"obj2"};

我想将结果串联为:

 {
    {"name":"obj1"},
    {"name":"obj2"}
 } 

阅读 JsonRepresentation,除了进行一些字符串操作外,没有简单的方法可以做到这一点。我说的对吗?

谢谢

【问题讨论】:

  • @portoalet,如果您提到的 JsonRepresentation 是 RestLet 库的一部分,请这样标记问题。

标签: json concatenation restlet


【解决方案1】:

如果你指的是this JsonRepresentation class,并且你想将这两个对象合并成一个数组,那么你应该可以这样做:

JSONObject jsonObj1 = obj1.toJsonObject();
JSONObject jsonObj2 = obj2.toJsonObject();
JSONArray jsonArray = new JSONArray().append(jsonObj1).append(jsonObj2);
JsonRepresentation jsonConcat = new JsonRepresentation(jsonArray);

注意:我实际上并没有使用过这个库,但如果它按照 API 运行,这应该很简单。

【讨论】:

  • 谢谢!我以为我可以在不使用 JSONArray 的情况下做任何事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
  • 2012-11-08
  • 1970-01-01
  • 2011-08-15
相关资源
最近更新 更多