zoukankan      html  css  js  c++  java
  • 取得远端相应Json并转化为Java对象(嵌套对象)二

    工程下载链接:https://files.cnblogs.com/files/xiandedanteng/JsonParse20190929.rar

    客户端

    如果从Restful Service取得的Json文是嵌套的,那转化的对象也会嵌套,用net.sf.json.JSONObject依然可以解析,但是得说明子对象的类型。

    比如远端的响应是{"name":"大明","emps":[{"id":1,"name":"傅宗龙"},{"id":2,"name":"孙传庭"},{"id":5,"name":"卢象升"}]}

    这里存在对象嵌套,需要在转化前指定里面的{"id":1,"name":"傅宗龙"}是什么类型的。

    具体代码如下:

            // 从远端取得相应
            RestTemplate rt=new RestTemplate();
            String result = rt.getForObject("http://localhost:8080/company",String.class);
            System.out.println(result);
            
            // 将Json字符串转嵌套Java对象
            Map<String,Class> classMap = new HashMap<String,Class>();
            classMap.put("emps", Emp.class);//这句话是告诉JSON解析器Company类的emps中元素是什么类型的,如果不指明,emps中会是恼人的MorphDynaBean类型的。
            
            JSONObject jsonObject=JSONObject.fromObject(result);
            Company stu=(Company)JSONObject.toBean(jsonObject, Company.class,classMap);
            System.out.println(stu);

    输出是这样:

    {"name":"大明","emps":[{"id":1,"name":"傅宗龙"},{"id":2,"name":"孙传庭"},{"id":5,"name":"卢象升"}]}
    公司名=大明 雇员名:傅宗龙 雇员名:孙传庭 雇员名:卢象升

    第二条说明Json文字到对象的转化是成功的。

    而Emp的代码是:

    package com.testEmp;
    
    public class Emp {
    
        private long id;
    
        private String name;
        
        public Emp() {
    
        }
        
        public Emp(long id,String name) {
            this.id=id;
            this.name=name;
        }
    
        public long getId() {
            return id;
        }
    
        public void setId(long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

    Company的代码是:

    package com.testEmp;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Company {
        private String name;// 雇员列表
    
        private List<Emp> emps;// 公司名称
        
        // 因为反射生成实例的缘故,这个空构造函数是必须的
        public Company() {
        }
        
        public Company(String name) {
            this.name=name;
            emps=new ArrayList<Emp>();
        }
        
        // 这种写法能做到连加
        public Company add(Emp newEmployee){
            emps.add(newEmployee);
            return this;
        }
        
        public String toString() {
            StringBuilder sb=new StringBuilder();
            
            sb.append("公司名="+this.name);
            
            for(Emp e:emps) {
                sb.append(" 雇员名:"+e.getName());
            }
            
            return sb.toString();
        }
        
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<Emp> getEmps() {
            return emps;
        }
    
        public void setEmps(List<Emp> emps) {
            this.emps = emps;
        }
    }

    服务端

    控制器:

    package com.example.demo;
    
    import java.util.Optional;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class Ctrl {
    。。。
        
        @RequestMapping("/company")
        public Company getCompany() {
            Company c=new Company("大明");
            
            c.add(new Emp(1,"傅宗龙")).add(new Emp(2,"孙传庭")).add(new Emp(5,"卢象升"));
            
            return c;
        }
    }

    Company类:

    package com.example.demo;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class Company {
        private String name;// 雇员列表
    
        private List<Emp> emps;// 公司名称
        
        public Company(String name) {
            this.name=name;
            emps=new ArrayList<Emp>();
        }
        
        // 这种写法能做到连加
        public Company add(Emp newEmployee){
            emps.add(newEmployee);
            return this;
        }
        
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<Emp> getEmps() {
            return emps;
        }
    
        public void setEmps(List<Emp> emps) {
            this.emps = emps;
        }
    }

    Emp类:

    package com.example.demo;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity
    public class Emp {
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private long id;
        
        @Column
        private String name;
        
        public Emp() {
    
        }
        
        public Emp(long id,String name) {
            this.id=id;
            this.name=name;
        }
    
        public long getId() {
            return id;
        }
    
        public void setId(long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }

    参考资料:https://blog.csdn.net/fenfenguai/article/details/78614788

    --END-- 2019年9月29日14:31:43

  • 相关阅读:
    UILabel标签文字过长时的显示方式
    iOS8新特性之交互式通知
    iOS 音频学习
    UISegmentedControl小常识和图片拉伸
    iOS 锁屏判断
    UIwindow的学习
    Mac显示和隐藏系统的隐藏文件
    获取iOS系统版本和设备的电量
    React Native 学习-01
    如何用fir.im 命令行工具 打包上传
  • 原文地址:https://www.cnblogs.com/heyang78/p/11607715.html
Copyright © 2011-2022 走看看