Java 如何使用 Jackson 注释将嵌套值映射到属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37010891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to map a nested value to a property using Hymanson annotations?
提问by kenske
Let's say I'm making a call to an API that responds with the following JSON for a product:
假设我正在调用一个 API,该 API 以产品的以下 JSON 进行响应:
{
"id": 123,
"name": "The Best Product",
"brand": {
"id": 234,
"name": "ACME Products"
}
}
I'm able to map the product id and name just fine using Hymanson annotations:
我可以使用 Hymanson 注释很好地映射产品 ID 和名称:
public class ProductTest {
private int productId;
private String productName, brandName;
@JsonProperty("id")
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
@JsonProperty("name")
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
}
And then using the fromJson method to create the product:
然后使用 fromJson 方法创建产品:
JsonNode apiResponse = api.getResponse();
Product product = Json.fromJson(apiResponse, Product.class);
But now I'm trying to figure out how to grab the brand name, which is a nested property. I was hoping that something like this would work:
但现在我想弄清楚如何获取品牌名称,这是一个嵌套属性。我希望这样的事情会奏效:
@JsonProperty("brand.name")
public String getBrandName() {
return brandName;
}
But of course it didn't. Is there an easy way to accomplish what I want using annotations?
但当然没有。有没有一种简单的方法可以使用注释来完成我想要的?
The actual JSON response I'm trying to parse is very complex, and I don't want to have to create an entire new class for every sub-node, even though I only need a single field.
我试图解析的实际 JSON 响应非常复杂,我不想为每个子节点创建一个全新的类,即使我只需要一个字段。
采纳答案by Jacek Grobelny
You can achieve this like that:
你可以这样实现:
String brandName;
@JsonProperty("brand")
private void unpackNameFromNestedObject(Map<String, String> brand) {
brandName = brand.get("name");
}
回答by jeorfevre
Hi here is the complete working code.
嗨,这是完整的工作代码。
//JUNIT TEST CLASS
//JUNIT测试类
public class sof {
公共课软{
@Test
public void test() {
Brand b = new Brand();
b.id=1;
b.name="RIZZE";
Product p = new Product();
p.brand=b;
p.id=12;
p.name="bigdata";
//mapper
ObjectMapper o = new ObjectMapper();
o.registerSubtypes(Brand.class);
o.registerSubtypes(Product.class);
o.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
String json=null;
try {
json = o.writeValueAsString(p);
assertTrue(json!=null);
logger.info(json);
Product p2;
try {
p2 = o.readValue(json, Product.class);
assertTrue(p2!=null);
assertTrue(p2.id== p.id);
assertTrue(p2.name.compareTo(p.name)==0);
assertTrue(p2.brand.id==p.brand.id);
logger.info("SUCCESS");
} catch (IOException e) {
e.printStackTrace();
fail(e.toString());
}
} catch (JsonProcessingException e) {
e.printStackTrace();
fail(e.toString());
}
}
}
**// Product.class**
public class Product {
protected int id;
protected String name;
@JsonProperty("brand") //not necessary ... but written
protected Brand brand;
}
**//Brand class**
public class Brand {
protected int id;
protected String name;
}
//Console.log of junit testcase
//junit testcase的console.log
2016-05-03 15:21:42 396 INFO {"id":12,"name":"bigdata","brand":{"id":1,"name":"RIZZE"}} / MReloadDB:40
2016-05-03 15:21:42 397 INFO SUCCESS / MReloadDB:49
Complete gist : https://gist.github.com/jeorfevre/7c94d4b36a809d4acf2f188f204a8058
完整要点:https: //gist.github.com/jeorfevre/7c94d4b36a809d4acf2f188f204a8058
回答by Srikanth Lankapalli
To make it simple ..I have written the code ...most of it is self explanatory.
为了简单起见......我已经编写了代码......其中大部分是不言自明的。
Main Method
Main Method
package com.test;
import java.io.IOException;
import com.fasterxml.Hymanson.core.JsonParseException;
import com.fasterxml.Hymanson.databind.JsonMappingException;
import com.fasterxml.Hymanson.databind.ObjectMapper;
public class LOGIC {
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
String DATA = "{\r\n" +
" \"id\": 123,\r\n" +
" \"name\": \"The Best Product\",\r\n" +
" \"brand\": {\r\n" +
" \"id\": 234,\r\n" +
" \"name\": \"ACME Products\"\r\n" +
" }\r\n" +
"}";
ProductTest productTest = objectMapper.readValue(DATA, ProductTest.class);
System.out.println(productTest.toString());
}
}
Class ProductTest
Class ProductTest
package com.test;
import com.fasterxml.Hymanson.annotation.JsonProperty;
public class ProductTest {
private int productId;
private String productName;
private BrandName brandName;
@JsonProperty("id")
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
@JsonProperty("name")
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
@JsonProperty("brand")
public BrandName getBrandName() {
return brandName;
}
public void setBrandName(BrandName brandName) {
this.brandName = brandName;
}
@Override
public String toString() {
return "ProductTest [productId=" + productId + ", productName=" + productName + ", brandName=" + brandName
+ "]";
}
}
Class BrandName
Class BrandName
package com.test;
public class BrandName {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "BrandName [id=" + id + ", name=" + name + "]";
}
}
OUTPUT
OUTPUT
ProductTest [productId=123, productName=The Best Product, brandName=BrandName [id=234, name=ACME Products]]
回答by gogstad
You can use JsonPath-expressions to map nested properties. I don't think there's any official support (see thisissue), but there's an unofficial implementation here: https://github.com/elasticpath/json-unmarshaller
您可以使用 JsonPath 表达式来映射嵌套属性。我认为没有任何官方支持(请参阅此问题),但这里有一个非官方实现:https: //github.com/elasticpath/json-unmarshaller
回答by Sunny KC
This is how I handled this problem:
我是这样处理这个问题的:
Brand
class:
Brand
班级:
package org.answer.entity;
public class Brand {
private Long id;
private String name;
public Brand() {
}
//accessors and mutators
}
Product
class:
Product
班级:
package org.answer.entity;
import com.fasterxml.Hymanson.annotation.JsonGetter;
import com.fasterxml.Hymanson.annotation.JsonIgnore;
import com.fasterxml.Hymanson.annotation.JsonSetter;
public class Product {
private Long id;
private String name;
@JsonIgnore
private Brand brand;
private String brandName;
public Product(){}
@JsonGetter("brandName")
protected String getBrandName() {
if (brand != null)
brandName = brand.getName();
return brandName;
}
@JsonSetter("brandName")
protected void setBrandName(String brandName) {
if (brandName != null) {
brand = new Brand();
brand.setName(brandName);
}
this.brandName = brandName;
}
//other accessors and mutators
}
Here, the brand
instance will be ignored by Hymanson
during serialization
and deserialization
, since it is annotated with @JsonIgnore
.
在这里,该brand
实例将被Hymanson
duringserialization
和忽略deserialization
,因为它是用 注释的@JsonIgnore
。
Hymanson
will use the method annotated with @JsonGetter
for serialization
of java object into JSON
format. So, the brandName
is set with brand.getName()
.
Hymanson
将使用java 对象的@JsonGetter
for注释的方法serialization
转换为JSON
格式。因此,brandName
设置为brand.getName()
。
Similarly, Hymanson
will use the method annotated with @JsonSetter
for deserialization
of JSON
format into java object. In this scenario, you will have to instantiate the brand
object yourself and set its name
property from brandName
.
同样,Hymanson
将使用@JsonSetter
for deserialization
of JSON
format注解的方法转化为java对象。在这种情况下,您必须自己实例化brand
对象并name
从brandName
.
You can use @Transient
persistence annotation with brandName
, if you want it to be ignored by persistence provider.
如果您希望持久性提供程序忽略它,您可以将@Transient
持久性注释与 一起使用brandName
。
回答by Toseef Zafar
The best is to use setter methods:
最好是使用 setter 方法:
JSON:
JSON:
...
"coordinates": {
"lat": 34.018721,
"lng": -118.489090
}
...
setter method for lat or lng will look like:
lat 或 lng 的 setter 方法将如下所示:
@JsonProperty("coordinates")
public void setLng(Map<String, String> coordinates) {
this.lng = (Float.parseFloat(coordinates.get("lng")));
}
if you need to read both (as you normally would do) then use a custom method
如果您需要阅读两者(就像您通常会做的那样),请使用自定义方法
@JsonProperty("coordinates")
public void setLatLng(Map<String, String> coordinates){
this.lat = (Float.parseFloat(coordinates.get("lat")));
this.lng = (Float.parseFloat(coordinates.get("lng")));
}