如何将对象转换为JSON表示形式 [英] How do I convert an object to JSON representation

查看:83
本文介绍了如何将对象转换为JSON表示形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

奇怪的是,我没有找到这个.

Oddly enough, I didn't find this.

将对象转换为JSON字符串的最简单方法是什么? (对象图中的循环等边缘情况对我来说并不重要.让我们找到一种简单的类A的解决方案,其中包含一些B,C,D类对象和一些基元).

What's the simplest way to convert an object to a JSON string? (Edge cases like loops in the object graphs aren't of much interest to me. Let's find a solution to the simple case of class A that contains some objects of classes B,C,D and some primitives).

必须有基本的收藏支持.

Basic collection support is a must.

推荐答案

嘿,我发现/记住了我们已经在使用的内容.

Heh, I discovered/remembered what we are already using for this.

ObjectMapper

代码看起来像这样-超级简单:

The code looks like this - super simple:

Object obj = ...
String result = new ObjectMapper().writeValueAsString(obj);

这篇关于如何将对象转换为JSON表示形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆