org.codehaus.jackson.JsonGenerationException:无法写入数字,期望字段名称

时间:2015-03-10 10:24:32

标签: json spring jackson

您好我正在使用spring mvc应用程序,我需要序列化一个对象,以便通过ajax Post传递它。

我的bean课程:

@JsonSerialize(using = AgentSer.class)
public class AgentCust implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Long personneID;
    private String nom;
    private String prenom;
    private String matriculation;
    private String marche;
    private String compte;
    private String phone, mail, chat;

    public String getMarche() {
        return marche;
    }

    public void setMarche(String marche) {
        this.marche = marche;
    }

    public String getCompte() {
        return compte;
    }

    public void setCompte(String compte) {
        this.compte = compte;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getChat() {
        return chat;
    }

    public void setChat(String chat) {
        this.chat = chat;
    }

    public Long getPersonneID() {
        return personneID;
    }

    public void setPersonneID(Long personneID) {
        this.personneID = personneID;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public String getMatriculation() {
        return matriculation;
    }

    public void setMatriculation(String matriculation) {
        this.matriculation = matriculation;
    }
}

以及将序列化我的bean的类:

public class AgentSer extends JsonSerializer<AgentCust> {
    @Override
    public void serialize(AgentCust value, JsonGenerator jgen, SerializerProvider arg2) throws IOException, JsonProcessingException {
        // TODO Auto-generated method stub
        jgen.writeStartObject();
        jgen.writeNumber(value.getPersonneID());
        jgen.writeString(value.getMatriculation());
        jgen.writeString(value.getNom());
        jgen.writeString(value.getPrenom());
        jgen.writeString(value.getCompte());
        jgen.writeString(value.getMarche());
        jgen.writeString(value.getChat());
        jgen.writeString(value.getMail());
        jgen.writeString(value.getPhone());
        jgen.writeEndObject();
    }
}

在我的控制器中我使用我的课程:

                AgentCust ags ;
                // i set values here .
                ObjectMapper mapper = new ObjectMapper();
                String json = "";
                try { 

                    json = mapper.writeValueAsString(ags);
                } catch (Exception e) {
                    System.out.println(e);
                }

但最后我明白了:

org.codehaus.jackson.JsonGenerationException:无法写入数字,期望字段名称

请帮助。

1 个答案:

答案 0 :(得分:1)

为什么使用自定义序列化程序(错误,因为它不包含字段名称)。你的生活真的很复杂。

您可以像这样设置序列化选项(您也可以在静态块中设置它们):

final ObjectMapper mapper = new ObjectMapper();

/* 
 you can set them globally in a static block and reuse the mapper...  
 performance gain
*/
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
mapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false);
mapper.setSerializationInclusion(Include.NON_NULL);

其余代码是相同的(只需在AgentCust.class中添加构造函数以避免一些映射错误):

AgentCust ags = new AgentCust();
ags.setChat("chat1");
ags.setCompte("compte1");
ags.setMail("mail1");
ags.setMarche("marche1");
ags.setMatriculation("matriculation1");
ags.setNom("nom1");
ags.setPersonneID(123456L);
ags.setPhone("phone1");
ags.setPrenom("prenom1");

String json = "";
try {
    json = mapper.writeValueAsString(ags);
} catch (JsonProcessingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
System.out.println(json);

另一个奇怪的事情是你将pojo序列化为String。为什么不用JsonNode或ObjectNode?

public static ObjectNode convObjToONode(Object o) {
    StringWriter stringify = new StringWriter();
    ObjectNode objToONode = null;

    try {
        mapper.writeValue(stringify, o);
        objToONode = (ObjectNode) mapper.readTree(stringify.toString());
    } catch (JsonMappingException e) {
        Logger.error("ERROR MAPPING JSON from object!", e);
    } catch (JsonGenerationException e) {
        Logger.error("ERROR GENERATING JSON from object!", e);
    } catch (IOException e) {
        Logger.error("ERROR IO when writing JSON from object!", e);
    }

    Logger.debug("Object as ObjectNode : " + objToONode);
    return objToONode;
}