android – 使用jackson对象映射器将JSON数组转换为POJO

前端之家收集整理的这篇文章主要介绍了android – 使用jackson对象映射器将JSON数组转换为POJO前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这可能是一个重复的问题..请参阅此 link.
我能够将一个json对象映射到POJO.But如何使用相同的jackson框架将json对象的数组转换为pojo.
private void jsonToPojo(){
   ObjectMapper mapper=new ObjectMapper();

    try {
        User1 user1=mapper.readValue(readFromFile(),User1.class);
        User1[] user2=mapper.readValue(readFromFile(),User1[].class);
        System.out.println(user1);
        Toast.makeText(getApplicationContext(),"inside try",0).show();
    } catch (JsonParseException e) {
        // TODO Auto-generated catch block
        Log.i("Exception","jsonparseexception");
        e.printStackTrace();
    } catch (JsonMappingException e) {
        // TODO Auto-generated catch block
        Log.i("Exception","jsonmapping exception");
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.i("Exception","ioexception");
        e.printStackTrace();
    }
}

这是类用户对象.

public class User {
private int age = 23;
private String name = "amal";
private List<String> messages = new ArrayList<String>() {
    {
        add("hi");
        add("how");
        add("are u.");
    }
};

//getter and setter methods

@Override
public String toString() {
    return "User [age=" + age + ",name=" + name + "," +
            "messages=" + messages + "]";
}

这是我试图做的:(readFromFile()从文件获取json)

User1[] user2=mapper.readValue(readFromFile(),User1[].class);

jsonToPojo()仅适用于一个对象.但是,如果我尝试上面的代码行,它不会采取以下json:

[ 
 {
   "age":"23","messages":["hi","how","are u."],"name":"amal"
 },{
   "age":"98","messages":["Reply","my","question"],"name":"You"
 }
]

解决方法

尝试这样的事情:
public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
    User a = new User(23,"amal");
    ArrayList<String> message = new ArrayList<String>();
    message.add("m1");
    message.add("m2");
    a.setMessages(message);

    User b = new User(58,"pete");
    User[] ab = new User[] {a,b};

    ObjectMapper mapper = new ObjectMapper();

    try {
        String s1 = getJson1(a);
        System.out.println(s1);
        User user1 = mapper.readValue(s1,User.class);
        System.out.println(user1);

        System.out.println("----------------");

        String s2 = getJson2(ab);
        System.out.println(s2);
        User[] user2 = mapper.readValue(s2,User[].class);
        for (User u : user2)
            System.out.println(u);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

    private static String getJson1(User user) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(user);
    }

    private static String getJson2(User[] ab) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(ab);
    }

}

public class User {
    private int               age;
    private String            name;
    private ArrayList<String> messages;

    public User() {
        super();
    }

    public User(int age,String name) {
        this();
        this.age = age;
        this.name = name;
    }

    // ****************
    // Getter & Setter ....
    // ****************
}

你会得到这个输出

{"age":23,"name":"amal","messages":["m1","m2"]}
User [age=23,name=amal,messages=[m1,m2]]
----------------
[{"age":23,"m2"]},{"age":58,"name":"pete","messages":null}]
User [age=23,m2]]
User [age=58,name=pete,messages=null]
原文链接:https://www.f2er.com/android/309870.html

猜你在找的Android相关文章