jackson 将 json 反序列化为 java-objects [英] jackson deserialization json to java-objects

查看:31
本文介绍了jackson 将 json 反序列化为 java-objects的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于反序列化的 Java 代码,我正在尝试将 json 字符串转换为 java 对象.为此,我使用了以下代码:

Here is my Java code which is used for the de-serialization, i am trying to convert json string into java object. In doing so i have used the following code:

package ex1jackson;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class Ex1jackson {
public static void main(String[] args) {
   ObjectMapper mapper = new ObjectMapper();
try {
        String userDataJSON = "[{"id":"value11","name": "value12","qty":"value13"},"
                              + "{"id": "value21","name":"value22","qty": "value23"}]";
        product userFromJSON = mapper.readValue(userDataJSON, product.class);
        System.out.println(userFromJSON);
    } catch (JsonGenerationException e) {
        System.out.println(e);
        } catch (JsonMappingException e) {
       System.out.println(e);
    } catch (IOException e) {
    System.out.println(e);
    } 
}
}

和我的 product.java 类

and my product.java class

package ex1jackson;
public class product 
{
private String id;
private String name; 
private String qty; 

@Override
public String toString() {
    return "Product [id=" + id+ ", name= " + name+",qty="+qty+"]";
}
}

我收到以下错误.

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 
Unrecognized field "id" (class ex1jackson.product), not marked as ignorable (0 known properties: ]) at 
[Source: java.io.StringReader@16f76a8; line: 1, column: 8] (through reference chain: ex1jackson.product["id"]) 
BUILD SUCCESSFUL (total time: 0 seconds)

帮我解决这个问题

推荐答案

看起来您正在尝试从实际描述数组的 JSON 中读取对象.Java 对象被映射到带有花括号 {} 的 JSON 对象,但你的 JSON 实际上以方括号 [] 开始,指定一个数组.

It looks like you are trying to read an object from JSON that actually describes an array. Java objects are mapped to JSON objects with curly braces {} but your JSON actually starts with square brackets [] designating an array.

你实际上拥有的是一个List 为了描述泛型类型,由于Java 的类型擦除,你必须使用一个TypeReference.您的反序列化可以是: myProduct = objectMapper.readValue(productJson, new TypeReference>() {});

What you actually have is a List<product> To describe generic types, due to Java's type erasure, you must use a TypeReference. Your deserialization could read: myProduct = objectMapper.readValue(productJson, new TypeReference<List<product>>() {});

另外几个注意事项:您的类应该始终是 PascalCased.您的 main 方法可以是 public static void main(String[] args) throws Exception,它可以为您节省所有无用的 catch 块.

A couple of other notes: your classes should always be PascalCased. Your main method can just be public static void main(String[] args) throws Exception which saves you all the useless catch blocks.

这篇关于jackson 将 json 反序列化为 java-objects的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆