首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对象转换为json,如何修改属性键?

对象转换为json,如何修改属性键?
EN

Stack Overflow用户
提问于 2014-11-11 09:21:31
回答 1查看 100关注 0票数 0

我有个json

"weatherInfo":{“城市”:“北京”,"publishDate":"2014年3月4日","week":"星期二","tempRange":"8℃~-3℃","feelTemp":"10","time":"16:05","temp":"11","WD":"北风","WS":"2级","SD":"27%",“北风”:“晴”}

我的课是

代码语言:javascript
运行
复制
public class WeatherVO implements Serializable{

    private static final long serialVersionUID = 2348480036959754071L;

    @JsonProperty(value="weatherinfo")
    private WeatherInfoVO weatherInfoVO;
    @JsonIgnoreProperties(ignoreUnknown=true)
    public class WeatherInfoVO{
        //城市
        @JsonProperty(value="city")
        private String city;
        //发布日期
        private String publishDate;
        //发布时间
        @JsonProperty(value="time")
        private String publishTime;
        //星期
        private String week;
        //温度范围
        private String tempRange;
        //当前时刻温度
        @JsonProperty(value="temp")
        private String currentTemp;
        //风向
        @JsonProperty(value="WD")
        private String windDirection;
        //风力
        @JsonProperty(value="WS")
        private String windForce;
        //当前时刻湿度
        @JsonProperty(value="SD")
        private String currentHumidity;
        //体感温度
        private String feelTemp;

        //天气描述
        private String weatherDesc;
   }
}

我想将json转换为对象,例如:

代码语言:javascript
运行
复制
WeatherVO weatherVO = objectMapper.readValue (jsonString, WeatherVO.class);

我想返回json,使用org.springframework.http.converter.json.MappingJacksonHttpMessageConverter类,返回json是

"weatherInfo":{“城市”:“北京”,"publishDate":"2014年3月4日","week":"星期二","tempRange":"8℃~-3℃","feelTemp":"10","time":"16:05","temp":"11","WD":"北风","WS":"2级","SD":"27%", "weather1":"晴“}

但我想要回到

"weatherInfo":{“城市”:“北京”,“publishDate”:“2014年年3月4日”,"week":"星期二","tempRange":"8℃~-3℃","feelTemp":"10","publishTime":"16:05","currentTemp":"11","windDirection":"北风","windForce":"2级","currentHumidity":"27%", 天气:“晴”}

我怎么能这么做?谢谢你的建议和帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-12 05:25:24

您可以通过@JsonIgnore标记字段的getter来实现这一点,然后创建另一个获取字段并将其标记为@JsonProperty的方法。

我的建议是先重命名您的字段以避免混淆。我把它重新命名了,就像你的json一样。举个例子。

代码语言:javascript
运行
复制
  public class WeatherInfoVO{

       private String city;
       private String publishDate;
       private String week;
       private String tempRange;
       private String feelTemp;
       private String time;
       private String temp;
       private String WD;
       private String WS;
       private String SD;
       private String weather1;
      //getters and setters

    }

然后在setters方法中标记适当的@JsonProperty。

代码语言:javascript
运行
复制
    @JsonProperty(value="time")
    public void setTime(String time) {
     this.temp = temp;
    }

    @JsonProperty(value="temp")
    public void setTemp(String temp) {
        this.temp = temp;
    }

    @JsonProperty(value="WD")
    public void setWD(String WD) {
        this.WD = WD;
    }   

    @JsonProperty(value="WS")
    public void setWS(String WS) {
        this.WS = WS;
    }
    @JsonProperty(value="SD")
    public void setSD(String SD) {
        this.SD = SD;
    }
    @JsonProperty(value="weather1")
    public void setWeather1(String weather1) {
        this.weather1 = weather1;
    }
    //other setters here

在getter中,确保在要重命名的字段中添加@JsonIgnore。因为您声明它为@JsonIgnore,所以需要创建另一个getter并将其标记为@JsonProperty。只对要重命名的字段执行此操作,在您的示例中,字段仅为time、temp、WD、WS、SD和weather1。下面是一个例子。

代码语言:javascript
运行
复制
    @JsonIgnore
    public void getTime(){
     return time;
    }

    @JsonProperty(value="publishTime")
    public void getPublishTime(){
     return time;
    }

    @JsonIgnore
    public void getTemp(){
     return temp;
    }
    @JsonProperty(value="currentTemp")
    public void getCurrentTemp(){
     return temp;
    }

    @JsonIgnore
    public void getWD(){
     return WD;
    }

    @JsonProperty(value="windDirection")
    public void getWindDirection(){
     return WD;
    }

    @JsonIgnore
    public void getWS(){
     return WS;
    }

    @JsonProperty(value="windForce")
    public void getWindForce(){
     return WS;
    }

    @JsonIgnore
    public void getSD(){
     return SD;
    }

    @JsonProperty(value="currentHumidity")
    public void getCurrentHumidity(){
     return SD;
    }

    @JsonIgnore
    public void getWeather1(){
     return weather1;
    }

    @JsonProperty(value="weather")
    public void getWeather(){
     return weather1;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26861404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档