使用jackson进行json数据格式转换

时间:2021-02-28 14:06:24
private static final JsonFactory factory = new JsonFactory();

StringWriter jsonOut = new StringWriter(1000);
JsonGenerator generator = factory.createGenerator(jsonOut);
generator .writeStartObject(); generator.writeStringField("article","交易提醒");
generator.writeStringField("url",url);
write ObjectField("Name","张三","#000000",generator);
generator.writeEndObject();
generator.close();
json = jsonOut.toString();
private static void writeObjectField(String fieldName,String value, String color, JsonGenerator generator) throws IOException{

generator.writeObjectFieldStart(fieldName);
generator.writeStringField("value",value);
generator.writeStringField("color",color);
generator.writeEndObject();
}

使用jackson进行json格式的数据封装非常方便~不需要写很多map啦~