java 使用 jackson 创建简单的 JSON 结构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11110446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:50:56  来源:igfitidea点击:

Create simple JSON structure using Hymanson

javajsonHymanson

提问by user701254

I'd just like to create the Hymanson mapping equivalent of the below :

我只想创建与以下等效的 Hymanson 映射:

{\"isDone\": true}

I think I need to create a class like this :

我想我需要创建一个这样的类:

public class Status {

    private boolean isDone;

    public boolean isDone{
        return this.isDone;
    }

    public void setDone(boolean isDone){
        this.isDone = isDone;
    }
}

But how do I instatiate it and then write the JSON to a string ?

但是我如何设置它,然后将 JSON 写入字符串?

回答by pb2q

A problem with your example and Hymanson is the default choices of JSON property names: Hymanson will see isDoneand setDoneand choose doneas the JSON property name. You can override this default choice using the JsonPropertyannotation:

你的榜样和Hyman逊的一个问题是JSON属性名的默认选择:Hyman逊将看到isDonesetDone和选择done的JSON属性名。您可以使用JsonProperty注释覆盖此默认选择:

public class Status
{
    private boolean isDone;

    @JsonProperty("isDone")
    public boolean isDone()
    {
        return this.isDone;
    }

    @JsonProperty("isDone")
    public void setDone(boolean isDone)
    {
        this.isDone = isDone;
    }
}

Then:

然后:

Status instance = new Status();
String jsonString = null;

instance.setDone(true);
ObjectMapper mapper = new ObjectMapper();

jsonString = mapper.writeValueAsString(instance);

Now jsonStringcontains { "isDone" : true }. Note that you can also write the string to an OutputStreamusing ObjectMapper.writeValue(OutputStream, Object), or to a Writerusing ObjectMapper.writeValue(Writer, Object).

现在jsonString包含{ "isDone" : true }. 请注意,您还可以将字符串写入OutputStreamusing ObjectMapper.writeValue(OutputStream, Object)Writerusing ObjectMapper.writeValue(Writer, Object)

In this case you really only need the JsonPropertyannotation on either of your accessors, but not both. Just annotating isDonewill get you the JSON property name that you want.

在这种情况下,您实际上只需要JsonProperty在其中一个访问器上添加注释,而不是两者都需要。只需注释即可isDone获得所需的 JSON 属性名称。

An alternative to using the JsonPropertyannotation is to rename your accessors setIsDone/getIsDone. Then the annotations are unnecessary.

使用JsonProperty注释的另一种方法是重命名访问器setIsDone/getIsDone。那么注释是不必要的。

See the quick and dirty Hymanson tutorial: Hymanson in 5 minutes. Understanding of the specific properties came from looking through the docs for Hymanson annotations.

请参阅快速而肮脏的 Hymanson 教程:5 分钟内的 Hymanson。对特定属性的理解来自于查看 Hymanson 注释的文档。

回答by Istvan Devai

Right. The code needed:

对。需要的代码:

ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(new Status()));