zoukankan      html  css  js  c++  java
  • Jackson JSON Processor

    Jackson提供接口,可以再json和bean之间互相转换

    1. 一个例子

    public class JsonToJavaBean {
        public static void main(String[] args) {
            String str="{"student":[{"name":"leilei","age":23},{"name":"leilei02","age":23}]}";
            Student stu = null;
            List<Student> list = null;
            try {
                ObjectMapper objectMapper=new ObjectMapper();
                StudentList studentList=objectMapper.readValue(str, StudentList.class);
                 
                list=studentList.getStudent();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            for(Student s:list){
                System.out.println(s.getName()+"   "+s.getAge());
            }
        }
    }

    2. 第二个例子

    public static void main(String[] args) {
            ArrayList<Student> list=new ArrayList<Student>();
            Student s1=new Student();
            s1.setName("leilei");
            s1.setAge(23);
            Student s2=new Student();
            s2.setName("leilei02");
            s2.setAge(23);
            list.add(s1);
            list.add(s2);
             
            StringWriter str=new StringWriter();
             
            ObjectMapper objectMapper=new ObjectMapper();
            try {
                objectMapper.writeValue(str, list);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
            System.out.println(str);
        }

    我见到的用法

    public Session fromJson(String json) throws JsonParseException, JsonMappingException, IOException {
            return new ObjectMapper().readValue(json, Session.class);
        }

    总结

    1. json在java里是以string的形式存在的

    2. 转换需要的信息是原始的json数据和转换标准bean的定义

    3. 可以转换array和单个object

  • 相关阅读:
    用 .Net WebBrowser 控件获取POST数据
    yield再理解--绝对够透彻
    Keras 和 PyTorch 的对比选择
    Keras -Python编写的开源人工神经网络库
    Python 加密之 生成pyd文件
    FPN全解-特征金字塔网络
    RetinaNet(Focal Loss)
    Focal Loss for Dense Object Detection(Retina Net)
    ImageNet Classification-darknet
    Darknet
  • 原文地址:https://www.cnblogs.com/xinsheng/p/3899111.html
Copyright © 2011-2022 走看看