• 一个复杂Json的解析


     1 {
     2     "website": {
     3         "1": {
     4             "basic": {
     5                 "homepage": "http://pythontip.sinaapp.com/",
     6                 "homename": "Python之禅--大道至简"
     7             },
     8             "list": {
     9                 "2": {
    10                     "childpage": "http://pythontip.sinaapp.com/study/books/pythontutorial3-master",
    11                     "chilename": "Python 入门指南"
    12                 },
    13                 "3": {
    14                     "childpage": "http://pythontip.sinaapp.com/coding/code_oj",
    15                     "chilename": "挑战Python"
    16                 }
    17             }
    18         }
    19     }
    20 }

    我使用的是Google的Gson对其做解析,
    下载gson jar包

     1 import java.io.Serializable;
     2 
     3 public class ChildPage implements Serializable {
     4 
     5     /**
     6      * 
     7      */
     8     private static final long serialVersionUID = 1L;
     9     
    10     private String childpage;
    11     private String chilename;
    12     
    13     //getter and setter
    14 
    15     @Override
    16     public String toString() {
    17         return "ChildPage [childpage=" + childpage+ ", chilename=" + chilename + "]";
    18     }
    19 }
     1 import java.io.Serializable;
     2 
     3 public class ParentPage implements Serializable {
     4 
     5     /**
     6      * 
     7      */
     8     private static final long serialVersionUID = 1L;
     9     
    10     private String hostpage;
    11     private String homename;
    12     
    13         //getter and setter
    14     
    15     @Override
    16     public String toString() {
    17         return "ParentPage [homepage=" + homepage+ ", homename=" + homename + "]";
    18     }
    19 }
     1 import java.io.Serializable;
     2 import java.util.HashMap;
     3 
     4 public class ParentAndChildPage implements Serializable {
     5 
     6     
     7     /**
     8      * 
     9      */
    10     private static final long serialVersionUID = 1L;
    11     
    12     private ParentPage basic;
    13     private HashMap<String,ChildPage> list;
    14     
    15         //getter and setter
    16     
    17     @Override
    18     public String toString() {
    19         return "ParentAndChildPage [basic=" + basic + ", list=" + list + "]";
    20     }
    21 }
     1 import java.io.Serializable;
     2 import java.util.HashMap;
     3 
     4 public class Website implements Serializable {
     5 
     6     /**
     7      * 
     8      */
     9     private static final long serialVersionUID = 1L;
    10     
    11     private HashMap<String,ParentAndChildPage> website;
    12 
    13     //getter and setter
    14 
    15     @Override
    16     public String toString() {
    17         return "Website [website=" + website + "]";
    18     }
    19 }
    1 Website website = new Gson().fromJson(json, Website.class);

    注意:Java Bean的属性必须和Json串的key值对应。

  • 相关阅读:
    Mac上如何用命令行修改proxy设置
    Mac上解决访问github慢问题
    Bootstrap布局
    ListView详解
    sql server命名规范
    表的管理与操作
    常用编程技巧和方法
    有联系的jQuery选择器
    sql基础查询语句
    数值特征
  • 原文地址:https://www.cnblogs.com/javagoboy/p/3677937.html
Copyright © 2020-2023  润新知