JSON序列化/反序列化,指定自定义字段名key转换注解配置

62 篇文章 3 订阅
37 篇文章 2 订阅

正常属性序列化和反序列化

实体类案例:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class User implements Serializable{
    private String name;
    private Integer age;
}

正常序列化和反序列化JSON对应为:

{"age":10,"name":"小明"}

案例:

import com.alibaba.fastjson.JSONObject;

// JSON反序列化为对象,再将对象序列化为JSON输出
String str = "{\"age\":10,\"name\":\"小明\"}";
User user = JSONObject.parseObject(str, User.class);
System.out.println(JSONObject.toJSONString(user));

将对象序列化为json,指定序列化后的属性名

实体类的属性上面增加@JsonProperty(“字段名”)注解(fastjson)

import com.fasterxml.jackson.annotation.JsonProperty;

@JsonProperty("姓名")
private String name;
@JsonProperty("年龄")
private Integer age;

在springboot请求接口返回对象得到的结果:(因为springboot使用fastjson序列化对象)

{"年龄":10,"姓名":"小明"}

将json反序列化为对象,指定json中key对应实体类的属性

json字符串为:

{"年龄":10,"姓名":"小明"}

实体类属性上增加@JSONField(name = “属性名”)注解(alibaba.fastjson)

import com.alibaba.fastjson.annotation.JSONField;

@JSONField(name = "姓名")
private String name;
@JSONField(name = "年龄")
private Integer age;

代码实现:

String str = "{\"年龄\":10,\"姓名\":\"小明\"}";
User user = JSONObject.parseObject(str, User.class);
System.out.println(JSONObject.toJSONString(user));

输出结果: 可以看到直接输出key也是自定义的,但这里用的是alibaba.fastjson注解完成的,和上面使用场景不一样,因为springboot使用fastjson序列化对象

{"姓名":"小明","年龄":10}
  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果你想要将Key值为Integer或Long类型的Map序列化JSON字符串时,保留引号,你可以使用一个自定义的键序列化器来实现。以下是一个示例代码: ```java import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializerProvider; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) throws JsonProcessingException { Map<Long, String> map = new HashMap<>(); map.put(1L, "Value 1"); map.put(2L, "Value 2"); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer()); String jsonString = objectMapper.writeValueAsString(map); System.out.println(jsonString); } static class NullKeySerializer extends JsonSerializer<Object> { @Override public void serialize(Object nullKey, JsonGenerator jsonGenerator, SerializerProvider unused) throws IOException, JsonProcessingException { jsonGenerator.writeFieldName(nullKey.toString()); } } } ``` 在上述示例中,我们创建了一个NullKeySerializer类作为自定义的键序列化器。它将键序列化为字符串,并将其写入JsonGenerator中作为字段名。 请注意,由于Map的键类型是Long或Integer,我们需要将其转换为字符串以满足JSON规范中键必须是字符串的要求。因此,在NullKeySerializer中,我们调用了nullKey.toString()方法来获取字符串形式的键。 这样,当将Map序列化JSON字符串时,键将带有引号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摘星喵Pro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值