zoukankan      html  css  js  c++  java
  • 把json数据转化成对象

    把json数据转化到一个对象中,再用对象直接调用

     1 package com.lxj.register;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.InputStreamReader;
     6 import java.net.MalformedURLException;
     7 import java.net.URL;
     8 
     9 import net.sf.json.JSONObject;
    10 
    11 public class Demo {
    12     public static void main(String[] args) {
    13         try {
    14             // 创建URL连接到服务器
    15             URL url = new URL("http://192.168.18.249:8080/order/register");
    16             try {
    17                 // 创建输入流
    18                 BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
    19                 StringBuffer sb = new StringBuffer();
    20                 String st ;
    21                 // 开始接受数据,先接受到st中
    22                 while((st = br.readLine()) != null){
    23                     // 再拼接到sd中
    24                     sb.append(st);
    25                 }
    26                 // 打印出所有的数据
    27                 System.out.println(sb.toString());
    28                 // JSONObject.toBean()把JSONObject转化成Register对象
    29                 // JSONObject.fromObject(sb.toString()把sb.toString字符串转化为JsonObject
    30                 // Register.class意思是转化成Register
    31                 Register register = (Register) JSONObject.toBean(JSONObject.fromObject(sb.toString()),Register.class);
    32                 // 服务器的数据存入了Register对象中,只要register调用就可以了
    33                 System.out.println("msg:"+register.getMsg());
    34                 System.out.println("retCode:"+register.getRetCode());
    35                 System.out.println("姓名:"+register.getInfo().getName());
    36                 System.out.println("性别:"+register.getInfo().getSex());
    37             } catch (IOException e) {
    38                 // TODO Auto-generated catch block
    39                 e.printStackTrace();
    40             }
    41         } catch (MalformedURLException e) {
    42             // TODO Auto-generated catch block
    43             e.printStackTrace();
    44         }
    45     }
    46 }
     1 package com.lxj.register;
     2 
     3 public class Register {
     4     private String msg;
     5     private String retCode;
     6     private Info info;
     7     
     8     // 生成set get 方法
     9     public String getMsg() {
    10         return msg;
    11     }
    12 
    13 
    14     public void setMsg(String msg) {
    15         this.msg = msg;
    16     }
    17 
    18 
    19     public String getRetCode() {
    20         return retCode;
    21     }
    22 
    23 
    24     public void setRetCode(String retCode) {
    25         this.retCode = retCode;
    26     }
    27 
    28 
    29     public Info getInfo() {
    30         return info;
    31     }
    32 
    33 
    34     public void setInfo(Info info) {
    35         this.info = info;
    36     }
    37 
    38     // 把Info创建成内部类,外部类可以直接调用
    39     public static class Info {
    40         private String name;
    41         private String sex;
    42         public String getName() {
    43             return name;
    44         }
    45         public void setName(String name) {
    46             this.name = name;
    47         }
    48         public String getSex() {
    49             return sex;
    50         }
    51         public void setSex(String sex) {
    52             this.sex = sex;
    53         }
    54         
    55     }
    56 }
  • 相关阅读:
    hello world!
    react(一):组件的生命周期
    position和BFC
    继承
    绕不开的this
    js世界这么大,闭包想看看
    js中数组常用方法总结
    Appium混合应用测试
    手机APP兼容性测试
    运行monitor提示需要安装旧JAVA SE 6运行环境
  • 原文地址:https://www.cnblogs.com/lxjhoney/p/6384102.html
Copyright © 2011-2022 走看看