"He-6ren">
gpt4 book ai didi

Java - 将对象转换为映射以在 JSON 中使用

转载 作者:行者123 更新时间:2023-12-02 07:46:53 26 4
gpt4 key购买 nike

我有一个对象,我们就调用它

class Foo {
String greeting = "Hello";
int age = 21;
}

我想转换为像 "greeting"=> "Hello", "age"=> 21 这样的 map 这将用作 JSONSerializer.toJSON( arg)

像 Perl 和 Python 这样的语言非常擅长这种交换。在 Java 中执行此操作的最佳实践方法是什么?

最佳答案

您不需要在序列化之前进行此转换。

Gson 会直接使用你的对象并使其成为 JSON 映射。

使用这个例子:

Gson gson = new GsonBuilder().create();
writer.write(gson.toJson(foo)); // foo is an instance of Foo

关于Java - 将对象转换为映射以在 JSON 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10705925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com