Witam,
package com.m.jackson_samples;
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
PlainMessage msg = new PlainMessage("Wiadomosc", "Pewna wiadomosc");
// write JSON from java object
mapper.writeValue(new File("E:\\msg.json"), msg);
// convert JSON to java object
// w tej linijce sie wywala
PlainMessage n = mapper.readValue("E:\\msg.json", PlainMessage.class);
}
}
package com.m.jackson_samples;
public abstract class MessangerData {
protected int id;
MessangerData(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
package com.m.jackson_samples;
public class PlainMessage extends MessangerData {
protected String author;
protected String content;
PlainMessage(String author, String content) {
super(1);
this.author = author;
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
Treść wyjątku:
Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unexpected character ('E' (code 69)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: java.io.StringReader@7a8a44a6; line: 1, column: 2]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1378)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:599)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:520)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleUnexpectedValue(ReaderBasedJsonParser.java:1387)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:678)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:2834)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2781)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1942)
at com.margor.jackson_samples.Main.main(Main.java:19)
W czym lezy problem, w przykladzie robili tak samo:
http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
Pozdrawiam i dziekuje za wszelkie zainteresowanie,