我们可以使用writerWithDefaultPrettyPrinter() 方法对JSON进行漂亮的打印,这是ObjectMapper 类的一个工厂方法,用于构造ObjectWriter ,它将使用默认的缩进pretty printer来序列化对象。
public ObjectWriter writerWithDefaultPrettyPrinter()
import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; public class PrettyPrintJsonJacksonTest { public static void main(String[] args) throws IOException { String data = "{\"Age\":30,\"Technologies\": [\"Java\",\"Spark\",\"Python\"],\"Name\":\"Adithya\"}"; ObjectMapper mapper = new ObjectMapper(); Object json = mapper.readValue(data, Object.class); String jsonStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json); // Pretty print JSON System.out.println(jsonStr); } }
{ "Age" : 30, "Technologies" : [ "Java", "Spark", "Python" ], "Name" : "Adithya" }
以上就是在Java中使用Jackson库漂亮地打印JSON?的详细内容,更多请关注php中文网其它相关文章!
已抢186837个
抢已抢67387个
抢已抢353387个
抢已抢667800个
抢已抢123032个
抢已抢24630个
抢