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进行数据的封装非常方便~
时间: 2024-10-13 02:27:48