我如何将两个相同的 JSON 属性名称分别赋值给两个不同的字段?

huangapple go评论87阅读模式
英文:

How do I have two identical json property names assigned to two separated fields

问题

{
  "content": "我有以下的Java类\n\n\n  [![enter image description here][1]][1]\n\n\n\n\n\n需要按以下方式将其序列化为json:\n\n如果列表(paymentTransactionReport字段)不为null,则显示其值 -\n\n{ \n    \"paymentTransactionResponse\" : [\n        {},\n        {}\n      ]\n}\n\n如果列表为null,则需要在json字段中显示paymentTransactionReportError,并将其命名为'paymentTransactionResponse',与前面的情况一样。示例 -\n\n{\n    \"paymentTransactionResponse\" : {\n    ----\n    //来自PaymentTransactionReportError类的字段\n    ----\n      }\n}\n\n如何做到这一点?最好不使用自定义序列化程序。\n如果只使用两个具有相同名称的注释@JsonProperty和JsonInclude.NON_NULL,就像我所做的那样,我会遇到此错误:找不到类型为...的返回值的转换器:...似乎是由于具有相同名称的字段在序列化过程中引发了错误。"
}
英文:

I have the following Java class 我如何将两个相同的 JSON 属性名称分别赋值给两个不同的字段?

I need to serialize it to json in the following way:

if the list(paymentTransactionReport field) is not null display it values -

{ 
"paymentTransactionResponse" : [
    {},
    {}
  ]
}

if the list is null I need to display the paymentTransactionReportError in json field with name 'paymentTransactionResponse', as in previous case. Example -

{
"paymentTransactionResponse" : {
----
//fields from PaymentTransactionReportError class
----
  }
}

How can I do this?preferably without custom serializers.
If use just two annotations @JsonProperty with the same name and JsonInclude.NON_NULL as I did, I have this error: No converter found for return value of type:... Seems to be that is a error happened during serialization because of fields with the same name

答案1

得分: 1

以下是翻译好的代码部分:

一种实现方法是使用 [@JsonAnyGetter][1]请尝试以下代码

public class TestDTO {

    @JsonIgnore
    List<String> paymentTransactionResponse;
    
    @JsonIgnore
    String paymentTransactionResponseError;

    public List<String> getPaymentTransactionResponse() {
        return paymentTransactionResponse;
    }

    public void setPaymentTransactionResponse(List<String> paymentTransactionResponse) {
        this.paymentTransactionResponse = paymentTransactionResponse;
    }

    public String getPaymentTransactionResponseError() {
        return paymentTransactionResponseError;
    }

    public void setPaymentTransactionResponseError(String paymentTransactionResponseError) {
        this.paymentTransactionResponseError = paymentTransactionResponseError;
    }

    @JsonAnyGetter
    public Map<String, Object> getData(){
        Map<String, Object> map = new HashMap<String, Object>();
        if(paymentTransactionResponse != null) {
            map.put("paymentTransactionResponse", paymentTransactionResponse);
        } else {
            map.put("paymentTransactionResponse", paymentTransactionResponseError);
        }
        return map;
    }
}
  
[1]: https://fasterxml.github.io/jackson-annotations/javadoc/2.9/com/fasterxml/jackson/annotation/JsonAnyGetter.html
英文:

One way you can achieve this is, using @JsonAnyGetter, Try this

public class TestDTO {

@JsonIgnore
List&lt;String&gt; paymentTransactionResponse;

@JsonIgnore
String paymentTransactionResponseError;

public List&lt;String&gt; getPaymentTransactionResponse() {
	return paymentTransactionResponse;
}

public void setPaymentTransactionResponse(List&lt;String&gt; paymentTransactionResponse) {
	this.paymentTransactionResponse = paymentTransactionResponse;
}

public String getPaymentTransactionResponseError() {
	return paymentTransactionResponseError;
}

public void setPaymentTransactionResponseError(String paymentTransactionResponseError) {
	this.paymentTransactionResponseError = paymentTransactionResponseError;
}

@JsonAnyGetter
public Map&lt;String, Object&gt; getData(){
	Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
	if(paymentTransactionResponse != null) {
		map.put(&quot;paymentTransactionResponse&quot;, paymentTransactionResponse);
	}else {
		map.put(&quot;paymentTransactionResponse&quot;, paymentTransactionResponseError);
	}
	return map;
}}

huangapple
  • 本文由 发表于 2020年9月14日 18:02:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63882140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定