Java的Jsonproperty

发布于 2025-01-28 11:35:05 字数 679 浏览 1 评论 0原文

我在其他问题中发现了这个问题,但我认为它们太老了,他们不再工作了。

我正在消耗返回JSON的外部服务,并且想将其转换为我自己的服务中的对象。同时,此对象将是我的服务输出。

我的问题是使用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;
  }

}

因此,我期望收到的是一个带有关键名称的JSON,但是我得到了一个关键名称,

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

因为我可以在其他解决方案中阅读的问题是导入,有些人正在使用旧的jsonproperty库,但是我正在使用期望的一个。

我还要检查什么可以使它起作用?

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. 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.

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

// 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.

Is there anything else I should check to make it work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日 2025-02-04 11:35:05

因此,您正在使用注释,使响应将“名称”作为钥匙。这完全可以重命名JSON响应键。因此,删除它会让您将钥匙作为“名称”获取

So you are using an annotation making the response to have that "NAME" as a key. This is solely there to rename the JSON response keys. So removing it will let you get the key as "name"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文