小编Say*_*yem的帖子

如何使用jackson将Json数组写入文件

我创建了一个Json文件,我想写java对象作为Array元素.我用杰克逊.

    try{
           String json;
           String phyPath = request.getSession().getServletContext().getRealPath("/");
           String filepath = phyPath + "resources/" + "data.json";
           File file = new File(filepath);
           if (!file.exists()) {
               System.out.println("pai nai");
               file.createNewFile();               
           }  
           json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(story);
           Files.write(new File(filepath).toPath(), Arrays.asList(json), StandardOpenOption.APPEND);    
    } 
Run Code Online (Sandbox Code Playgroud)

这不是我想要的.它创建数据

{
  "storyTitle" : "ttt",
  "storyBody" : "tttt",
  "storyAuthor" : "tttt"
}
{
  "storyTitle" : "a",
  "storyBody" : "a",
  "storyAuthor" : "a"
}
Run Code Online (Sandbox Code Playgroud)

我只需要创建一个Json数组,我在其中添加java对象,数据应该是这样的

[{
  "storyTitle" : "ttt",
  "storyBody" : "tttt",
  "storyAuthor" : "tttt"
}
,{
  "storyTitle" : "a",
  "storyBody" : "a",
  "storyAuthor" : …
Run Code Online (Sandbox Code Playgroud)

java json jackson

15
推荐指数
2
解决办法
2万
查看次数

在Groovy中将String转换为arrayList

我有一个字符串:String str = "[aa,bb,cc,dd]".我想将其转换为groovy中的列表[aa, bb, cc, dd].任何可用于此类型转换的groovy方法?

grails groovy

3
推荐指数
2
解决办法
6557
查看次数

如何在 Grails 3.2.3 中使用 ajax

我在 grails 2.4.7 工作。最近我转移到 grails 3.Previously 我使用 grails 自定义标签进行 ajax 调用,如 remoteFunction、formRemote。这个标签不适用于最新版本。谁能告诉我使用 ajax 的最佳方法最新版本。

ajax grails taglib ajaxtags grails3

2
推荐指数
1
解决办法
1327
查看次数

标签 统计

grails ×2

ajax ×1

ajaxtags ×1

grails3 ×1

groovy ×1

jackson ×1

java ×1

json ×1

taglib ×1