java - 我们如何才能在 json 中隐含

标签 java json xstream

我们是否有任何注释可以替代 JSON 的 @xstreamImplicit,因为 XstreamImplicit 仅适用于 XML?

例如:

Class A {

  @XstreamImplicit
  private List<Apple> apples;
}

XML:

<apple>
  .....
  .....
</apple>
<apple>
   ....
   ....
</apple>

JSON:

{ apples: [{...}] }

我想要以下格式的所需 JSON 输出:

[{...}]

最佳答案

根据"Write to JSON with the self-contained JSON driver dropping the root" in the JSON tutorial ,你可以这样写:

XStream xstream = new XStream(new JsonHierarchicalStreamDriver() {
    public HierarchicalStreamWriter createWriter(Writer writer) {
        return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE);
    }
});

关于java - 我们如何才能在 json 中隐含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13703694/

相关文章:

c++ - 段错误读取json文件

java - 如何在Java中的xml字符串中包含 "encoding="UTF-8""?

java - 如何停止 JShell/Kulla 中的无限循环?

C# JSON 响应执行第二个 JSON 请求

php - Json导入mysql

java - 使用 Xstream 解析模式位置的 XML

java - 如何限制java菜单中的用户操作?

Java volatile 关键字未按预期工作

java - 无法将数据发布到服务器,出现错误 java.io.IOException : Server returned HTTP response code: 415