将JSONArray写入文件,仅写入值的第一个集合

时间:2019-04-17 10:51:44

标签: java arrays json object arraylist

致意,并感谢您的宝贵时间 我遇到了一个奇怪的问题,尽管可以找到一个简单的解决方案,但我确定是一个简单的解决方案。

问题是这样的:执行以下代码时,print函数将打印jsonarray中的所有元素,但仅写入writer时,将打印元素的第一个集合。

任何提示都将不胜感激

到目前为止,我已经尝试了代码中显示的内容,并且遍历了列表

p3 <- ggplot(animated_data,
       aes(x = time_point, y = score, colour = condition, group = condition)) +

  # static layers (assuming 11 is the desired ending point)
  geom_segment(data = static_data,
               aes(x = 0, xend = 11, y = fixed_score, yend = fixed_score), 
               inherit.aes = FALSE, colour = "grey25") +
  geom_text_repel(data = static_data,
                  aes(x = 11, y = fixed_score, label = hline_label), 
                  hjust = 0, size = 4, direction = "y", box.padding = 1.0, inherit.aes = FALSE, 
                  seed = 123,           # set a constant random seed
                  xlim = c(11, NA)) +   # specify repel range to be from 11 onwards

  # animated layers (only specify additional aesthetic mappings not mentioned above)
  geom_point() +
  geom_line() +
  geom_segment(aes(xend = time_point, yend = score), linetype = 2) +
  geom_text(aes(x = max(time_point) + 1, label = condition),
            hjust = 0, size = 4) +

  # static aesthetic settings (limits / expand arguments are specified in coordinates
  # rather than scales, margin is no longer specified in theme since it's no longer
  # necessary)
  scale_x_continuous(breaks = seq(0, 10, by = 2)) +
  scale_y_continuous(breaks = seq(2, 3, by = 0.10)) + 
  scale_color_manual(values = condition_colours)  +
  coord_cartesian(xlim = c(0, 13), ylim = c(2, 3), expand = FALSE) +
  guides(col = F) +
  labs(title = "[Title Here]", x = "Time", y = "Mean score") + 
  theme_minimal() + 
  theme(panel.grid.minor = element_blank())  + 

  # animation settings (unchanged)
  transition_reveal(time_point) +
  ease_aes('linear') 

animate(p3, nframes = 50, end_pause = 5, height = 1000, width = 1250, res = 120)

我要写入文件的输出与标准输出相同:

  

[[标题-阿姆又回来了-盗版语言-eng   ReleaseDate-2004-09-28格式-CD轨道数-11]] [[标题-   阿姆表演状态-官方语言-英文发布日期-2002   格式-数字媒体曲目计数-20]] [[标题-阿姆放映节目   状态-官方语言-英文发布日期-2002格式-   数字媒体跟踪计数-19]]

再次,只写了这个:

  

[[标题-阿姆又回来了-盗版语言-eng   ReleaseDate-2004-09-28格式-CD轨道数-11]]

3 个答案:

答案 0 :(得分:0)

事实证明,如@SatyaTNV所述,解决方法很简单,唯一需要更改的是Filewriter行

通过添加

new FileWriter(filename, true)

一切都很好打印!谢谢基于SatyaTNV!

答案 1 :(得分:0)

尝试一下

try (PrintWriter pw = new PrintWriter(writer)) {
  pw.println(jsonArray.toString());
}

我不确定要按JSONArray类写作

答案 2 :(得分:0)

请检查我的解决方案: App.java class

// JSON解析器对象,用于解析读取的文件         JSONParser jsonParser =新的JSONParser();

try {
            URL resource = App.class.getClassLoader().getResource("info.json");
            System.out.println(resource.toString());
            FileReader reader = new FileReader(resource.getFile());

            // Read JSON file
            Object obj = jsonParser.parse(reader);

            JSONArray infoList = (JSONArray) obj;
            System.out.println(infoList);

            Information i = new Information();

            List<Information> info = i.parseInformationObject(infoList);

            saveInformation(info);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }

并迭代: Information.java class

List<Information> parseInformationObject(JSONArray infoList) {
        List<Information> in = new ArrayList<>();

        infoList.forEach(emp -> {

            JSONObject info = (JSONObject) emp;

            String id = info.get("id").toString();
            String state = info.get("state").toString();
            String type = null;
            if (info.get("type") != null) {
                type = info.get("type").toString();
            }
            String host = null;
            if (info.get("host") != null) {
                host = info.get("host").toString();
            }
            long timestamp = (long) info.get("timestamp");

            in.add(new Information(id, state, type, host, timestamp));

        });
        return in;
    }
相关问题