如何序列化地图<整数 ,自定义对象>与杰克逊流API?整数>
假设我有一个
class Person {
ArrayList<MyOtherObject> lstObjects;
...
}
然后
Map<Integer, Person> personMap
想要使用 Jackson Streaming API 序列化该地图?
JsonGenerator g =...;
g.writeArrayFieldStart("PersonMap");
if (personMap != null) {
Iterator<Map.Entry<Integer, Person>> iter = personMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<Integer, Person> pairs = iter.next();
Integer key = (Integer) pairs.getKey();
Person person = (Person) pairs.getValue();
g.writeNumber(key.intValue());
person.saveToFileRaw(g); // Write the object
}
}
g.writeEndArray(); // PersonMap
和 person.saveToFileRaw 看起来像
try {
g.writeStartObject();
g.writeObjectFieldStart("Inf");
if (lstInfo != null) {
for (PersonInfo info: lstInfo)
info.saveToFileRaw(g); // Write another object
}
g.writeEndObject();
String s = PersonType.token(type);
g.writeStringField("Tp", s);
g.writeStringField("Add", address);
所以问题:如何编写自定义对象的数组/映射? person.saveToFileRaw 中的 g.writeStartObject() 抛出异常,表示它需要一个值。
有什么想法如何做到这一点?
Say that I've got a
class Person {
ArrayList<MyOtherObject> lstObjects;
...
}
and then
Map<Integer, Person> personMap
and want to serialize that map with Jackson Streaming API?
JsonGenerator g =...;
g.writeArrayFieldStart("PersonMap");
if (personMap != null) {
Iterator<Map.Entry<Integer, Person>> iter = personMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<Integer, Person> pairs = iter.next();
Integer key = (Integer) pairs.getKey();
Person person = (Person) pairs.getValue();
g.writeNumber(key.intValue());
person.saveToFileRaw(g); // Write the object
}
}
g.writeEndArray(); // PersonMap
and person.saveToFileRaw looks like
try {
g.writeStartObject();
g.writeObjectFieldStart("Inf");
if (lstInfo != null) {
for (PersonInfo info: lstInfo)
info.saveToFileRaw(g); // Write another object
}
g.writeEndObject();
String s = PersonType.token(type);
g.writeStringField("Tp", s);
g.writeStringField("Add", address);
So the question: how to write an array/map of custom objects? g.writeStartObject() in person.saveToFileRaw throws an exception saying that it expects a value.
Any ideas how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您从 JsonGenerator 调用中收到异常,则说明您正在尝试创建无效的 JSON 结构;无法解析的东西。
我在代码中看到的一个问题是,您首先调用“g.writeObjectFieldStart("Inf")”,但然后在循环中尝试调用以“g.writeStartObject”开头的方法——本质上是尝试写入起始对象标记“{ “两次。
您还可以单独调用“writeFieldName”(而不是 writeObjectFieldStart()),您可能需要这样做。或者您可能需要对 PersonInfo 条目执行 writeStartArray(() / writeEndArray() ;这取决于您想要的确切输出。
If you get an exception from JsonGenerator calls, you are trying to create invalid JSON structure; something that could not be parsed.
One problem I see in the code is that you first call "g.writeObjectFieldStart("Inf")", but then in loop try to call method which starts with "g.writeStartObject" -- essentially trying write start-object marker "{" twice.
You can also call "writeFieldName" separately (instead of writeObjectFieldStart()) which you probably need to do. Or maybe you need to do writeStartArray(() / writeEndArray() for PersonInfo entries; this depends on what exact output you want.