使用Jackson序列化Java中的数组数组 [英] Serializing an array of arrays in java using Jackson

查看:553
本文介绍了使用Jackson序列化Java中的数组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用Jackson,并且必须序列化一系列ContoCorrente对象

I'm learning how to use Jackson and I have to serialize an array of ContoCorrente objects

public class ContoCorrente {
    private String proprietario;
    private ArrayList<MovimentoBancario> movimenti;
//methods...
}

其中MovimentoBancario定义为

where MovimentoBancario is defined as

public class MovimentoBancario {
    private String data;
    private String causale;
//methods...
}

我尝试过

ArrayList<ContoCorrente> conti= new ArrayList<ContoCorrente>(50);
        for(int i=0; i<50; i++){
            //fill array
        }
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
            System.out.println(mapper.writeValueAsString(conti));  
        } catch (Exception e) {
            e.printStackTrace();
        }

但输出是

[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]

我不知道我在做什么错,这是我第一次尝试序列化

I don't know what I'm doing wrong, it's the first time I try serialization

推荐答案

您可以尝试使用Jackson定义特定方法.

You can try defining a specific method using Jackson.

private static final ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectReader reader = new ObjectMapper().readerFor(Array.class);


public static String toArray(ArrayList<ContoCorrente> array) throws JsonProcessingException {
    return objectMapper.writeValueAsString(array);
}

然后,如果您想阅读,请使用阅读器.

Then if you want to read just use the reader.

这篇关于使用Jackson序列化Java中的数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆