简体   繁体   English

java中的JsonProperty

[英]JsonProperty in java

I found this question in other questions, but I think they are too old and they are not working anymore.我在其他问题中发现了这个问题,但我认为它们太旧了,它们不再工作了。

I'm consuming an external service that returns a JSON and I want to convert it into an object in my own service.我正在使用一个返回 JSON 的外部服务,并且我想将它转换为我自己的服务中的一个对象。 At the same time, this object will be my service output.同时,这个对象将是我的服务输出。

My problem is using JsonProperty, I would like to rename some variables I'm receiving in my json, but it is not working, somehow it works in debugger, but not in the returned object.我的问题是使用 JsonProperty,我想重命名我在 json 中收到的一些变量,但它不起作用,不知何故它在调试器中起作用,但在返回的对象中不起作用。

import com.fasterxml.jackson.annotation.JsonProperty;

public class MyClass {

  @JsonProperty("NAME")
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}

So what I expect to receive is a json with a key name, but I get one with key NAME所以我期望收到的是一个带有键名的json,但我得到了一个带有键名的json

// Expected:
{
   "name": "John"
}
// Received
{
   "NAME": "John"
}

As I can read in other solutions the problem is the import, some people are using old JsonProperty library, but I'm using the expecting one.正如我在其他解决方案中看到的那样,问题是导入,有些人正在使用旧的 JsonProperty 库,但我正在使用预期的库。

Is there anything else I should check to make it work?还有什么我应该检查以使其正常工作吗?

So you are using an annotation making the response to have that "NAME" as a key.因此,您正在使用注释做出响应以将该“NAME”作为键。 This is solely there to rename the JSON response keys.这仅用于重命名 JSON 响应键。 So removing it will let you get the key as "name"所以删除它会让你得到钥匙作为“名字”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM