Java 使用特定反序列化程序playframework 2.8进行jackson反序列化
我有一个特定的json来自prestashop webservoice,我无法使用playframework 2.8正确地进行反序列化Java 使用特定反序列化程序playframework 2.8进行jackson反序列化,java,json,playframework-2.0,json-deserialization,jackson2,Java,Json,Playframework 2.0,Json Deserialization,Jackson2,我有一个特定的json来自prestashop webservoice,我无法使用playframework 2.8正确地进行反序列化 { "orders": [ { "id": 44, "id_address_delivery": "95", "id_address_invoice": "95", "id_cart": "121", "id_
{
"orders": [
{
"id": 44,
"id_address_delivery": "95",
"id_address_invoice": "95",
"id_cart": "121",
"id_currency": "2",
"id_lang": "2",
"id_customer": "61",
"id_carrier": "19",
"current_state": "5",
"module": "stripe_official",
"invoice_number": "0",
"invoice_date": "0000-00-00 00:00:00",
"delivery_number": "41",
"delivery_date": "2020-01-19 23:16:05",
"valid": "1",
"date_add": "2020-01-19 18:38:10",
"date_upd": "2020-01-19 23:16:06",
"shipping_number": "",
"id_shop_group": "1",
"id_shop": "1",
"secure_key": "23e5bfbd764ab60bd23aa1c7d70e8e4e",
"payment": "Paiement par Stripe",
"recyclable": "0",
"gift": "0",
"gift_message": "",
"mobile_theme": "0",
"total_discounts": "0.000000",
"total_discounts_tax_incl": "0.000000",
"total_discounts_tax_excl": "0.000000",
"total_paid": "28.000000",
"total_paid_tax_incl": "28.000000",
"total_paid_tax_excl": "28.000000",
"total_paid_real": "28.000000",
"total_products": "28.000000",
"total_products_wt": "28.000000",
"total_shipping": "0.000000",
"total_shipping_tax_incl": "0.000000",
"total_shipping_tax_excl": "0.000000",
"carrier_tax_rate": "0.000",
"total_wrapping": "0.000000",
"total_wrapping_tax_incl": "0.000000",
"total_wrapping_tax_excl": "0.000000",
"round_mode": "2",
"round_type": "2",
"conversion_rate": "1.000000",
"reference": "JRRCSMHKI",
"associations": {
"order_rows": [
{
"id": "133",
"product_id": "45",
"product_attribute_id": "0",
"product_quantity": "1",
"product_name": "Kebab - Sandwich ou Menu: Sandwich seul; - Sauce: Biggy burger; - Crudités: Salade; Maïs",
"product_reference": "",
"product_ean13": "",
"product_isbn": "",
"product_upc": "",
"product_price": "5.500000",
"id_customization": "0",
"unit_price_tax_incl": "5.500000",
"unit_price_tax_excl": "5.500000"
}
]
}
}
]
}
问题是关联对象我真的不明白,因为最后我只想要一个订单列表,每个订单中都有一个产品列表。。。
我设法用这种方法得到了订单单,但后来我无法做到
我从这个开始,但产品无法反序列化
List<Order> orders= Json.mapper().readValue(json.get("orders").traverse(), new TypeReference<List<Order>>(){});
List orders=Json.mapper().readValue(Json.get(“orders”).traverse(),new-TypeReference(){});
我的班级就是这样
封装模型;
导入java.util.Date;
导入java.util.List;
导入com.fasterxml.jackson.annotation.JsonFormat;
导入com.fasterxml.jackson.annotation.JsonIgnoreProperties;
导入com.fasterxml.jackson.annotation.JsonProperty;
导入com.fasterxml.jackson.databind.annotation.JsonDeserialize;
导入utils.ProductDeserializer;
@JsonIgnoreProperties(ignoreUnknown=true)
公共阶级秩序{
私人长id;
私营国家;
私人字符串支付;
@JsonProperty(value=“交货日期”)
@JsonFormat(shape=JsonFormat.shape.STRING,pattern=“dd-MM-yyyy-HH:MM:ss”)
私人日期交货日期;
@JsonProperty(value=“已缴税款总额”)
私人支付;
@JsonDeserialize(使用=ProductDeserializer.class)
私人上市产品;
公共浮点getTotalPaid(){
已支付的报酬总额;
}
公共作废setTotalPaid(浮动totalPaid){
this.totalPaid=totalPaid;
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共状态getState(){
返回状态;
}
公共无效设置状态(状态){
this.state=状态;
}
公共字符串getPayment(){
退还款项;
}
公共付款(字符串付款){
这个付款=付款;
}
公共日期getDeliveryDate(){
退货日期;
}
公共作废setDeliveryDate(日期deliveryDate){
this.deliveryDate=deliveryDate;
}
公共列表产品(){
退货产品;
}
公共产品(列出产品){
这一点。产品=产品;
}
}
并没有调用反序列化程序,我尝试将对象关联到OrderRaws然后是product,但它是无用的类,我想要一些简单高效的东西。
有什么想法吗
package-utils;
导入java.io.IOException;
导入java.util.List;
导入com.fasterxml.jackson.core.JsonParser;
导入com.fasterxml.jackson.core.type.TypeReference;
导入com.fasterxml.jackson.databind.DeserializationContext;
导入com.fasterxml.jackson.databind.JsonNode;
导入com.fasterxml.jackson.databind.desr.std.StdDeserializer;
进口型号.产品;
导入play.libs.Json;
公共类ProductDeserializer扩展StdDeserializer{
/**
*
*/
私有静态最终长serialVersionUID=1L;
public ProductDeserializer(){
这个(空);
}
公共产品反序列化程序(类vc){
超级(vc);
}
@凌驾
公共列表反序列化(JSONP,反序列化上下文ctxt)引发IOException{
JsonNode node=p.getCodec().readTree(p);
返回Json.mapper().readValue(node.get(“associations”).get(“order_rows”).traverse(),new TypeReference(){});
}
}
在json中,名称
products
没有属性。
您应该将@JsonProperty(“关联”)
添加到订单
中的产品
属性中,如:
@JsonDeserialize(使用=ProductDeserializer.class)
@JsonProperty(“关联”)
私人上市产品;
然后更新ProductDeserializer
以遍历订单行
,而不是关联
返回Json.mapper().readValue(node.get(“order_rows”).traverse(),new TypeReference(){});
编辑:添加我的全部代码。我只使用Jackson,不使用带播放框架的Jackson
主要类别:
import java.util.List;
导入com.fasterxml.jackson.core.JsonProcessingException;
导入com.fasterxml.jackson.core.type.TypeReference;
导入com.fasterxml.jackson.databind.JsonNode;
导入com.fasterxml.jackson.databind.ObjectMapper;
公营索曼{
私有静态最终字符串jsonStr=“Your json”;
公共静态void main(字符串[]args){
ObjectMapper ObjectMapper=新的ObjectMapper();
试一试{
JsonNode parentNode=objectMapper.readTree(jsonStr);
List orders=objectMapper.convertValue(parentNode.get(“orders”),new-TypeReference(){});
系统输出打印项次(订单);
}捕获(JsonProcessingException e){
e、 printStackTrace();
}
}
}
产品:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
导入com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown=true)
公共类产品{
私有int-id;
@JsonProperty(“产品名称”)
私有字符串产品名称;
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getProductName(){
返回产品名称;
}
公共无效setProductName(字符串productName){
this.productName=productName;
}
}
ProductDeserializer:
import java.io.IOException;
导入java.util.List;
导入com.fasterxml.jackson.core.JsonParser;
导入com.fasterxml.jackson.core.type.TypeReference;
导入com.fasterxml.jackson.databind.DeserializationContext;
导入com.fasterxml.jackson.databind.JsonNode;
导入com.fasterxml.jackson.databind.ObjectMapper;
导入com.fasterxml.jackson.databind.desr.std.StdDeserializer;
公共类ProductDeserializer扩展