zoukankan      html  css  js  c++  java
  • 一种从JSON数据创建Java类的高效办法

    作者:chszs,转载需注明。博客主页:http://blog.csdn.net/chszs

    JSON格式的数据经常会遇到,比如调用Web服务,取回的数据通常就是JSON格式的。如何高效地把JSON数据转换成实际的Java类对象,就是本文要说明的问题。

    写一个操纵JSON数据的Java程序,通常代码会重度依赖于JSON API,你总是需要对JSON数据进行反序列化,再转换成原生Java对象。整个过程大致如下:

    1)下载所有的JSON响应;

    2)分析JSON对象的结构,映射到Java类;

    3)手动煞费苦心地创建每一个Java类,键入每个Java类的私有属性名和数据类型,以匹配JSON所有对象的属性;

    4)为每个Java类创建public类型的getter和setter方法。

     

    1. package com.cypressnorth.demo.models.twitter;  
    2.    
    3. import java.util.List;  
    4.    
    5. public class TwitterItem{  
    6.     private String contributors;  
    7.     private transient Geo coordinates;  
    8.     private String created_at;  
    9.     private Entities entities;  
    10.     private Number favorite_count;  
    11.     private boolean favorited;  
    12.     private Geo geo;  
    13.     private Number id;  
    14.     private String id_str;  
    15.     private String in_reply_to_screen_name;  
    16.     private String in_reply_to_status_id;  
    17.     private String in_reply_to_status_id_str;  
    18.     private String in_reply_to_user_id;  
    19.     private String in_reply_to_user_id_str;  
    20.     private String lang;  
    21.     private boolean possibly_sensitive;  
    22.     private Number retweet_count;  
    23.     private boolean retweeted;  
    24.     private Retweeted_status retweeted_status;  
    25.     private String source;  
    26.     private String text;  
    27.     private boolean truncated;  
    28.     private User user;  
    29.    
    30.     public TwitterItem(){}  
    31.    
    32.     public String getContributors(){  
    33.         return this.contributors;  
    34.     }  
    35.     public void setContributors(String contributors){  
    36.         this.contributors = contributors;  
    37.     }  
    38.     public Geo getCoordinates(){  
    39.         return this.coordinates;  
    40.     }  
    41.     public void setCoordinates(Geo coordinates){  
    42.         this.coordinates = coordinates;  
    43.     }  
    44.     public String getCreated_at(){  
    45.         return this.created_at;  
    46.     }  
    47.     public void setCreated_at(String created_at){  
    48.         this.created_at = created_at;  
    49.     }  
    50.     public Entities getEntities(){  
    51.         return this.entities;  
    52.     }  
    53.     public void setEntities(Entities entities){  
    54.         this.entities = entities;  
    55.     }  
    56.     public Number getFavorite_count(){  
    57.         return this.favorite_count;  
    58.     }  
    59.     public void setFavorite_count(Number favorite_count){  
    60.         this.favorite_count = favorite_count;  
    61.     }  
    62.     public boolean getFavorited(){  
    63.         return this.favorited;  
    64.     }  
    65.     public void setFavorited(boolean favorited){  
    66.         this.favorited = favorited;  
    67.     }  
    68.     public Geo getGeo(){  
    69.         return this.geo;  
    70.     }  
    71.     public void setGeo(Geo geo){  
    72.         this.geo = geo;  
    73.     }  
    74.     public Number getId(){  
    75.         return this.id;  
    76.     }  
    77.     public void setId(Number id){  
    78.         this.id = id;  
    79.     }  
    80.     public String getId_str(){  
    81.         return this.id_str;  
    82.     }  
    83.     public void setId_str(String id_str){  
    84.         this.id_str = id_str;  
    85.     }  
    86.     public String getIn_reply_to_screen_name(){  
    87.         return this.in_reply_to_screen_name;  
    88.     }  
    89.     public void setIn_reply_to_screen_name(String in_reply_to_screen_name){  
    90.         this.in_reply_to_screen_name = in_reply_to_screen_name;  
    91.     }  
    92.     public String getIn_reply_to_status_id(){  
    93.         return this.in_reply_to_status_id;  
    94.     }  
    95.     public void setIn_reply_to_status_id(String in_reply_to_status_id){  
    96.         this.in_reply_to_status_id = in_reply_to_status_id;  
    97.     }  
    98.     public String getIn_reply_to_status_id_str(){  
    99.         return this.in_reply_to_status_id_str;  
    100.     }  
    101.     public void setIn_reply_to_status_id_str(String in_reply_to_status_id_str){  
    102.         this.in_reply_to_status_id_str = in_reply_to_status_id_str;  
    103.     }  
    104.     public String getIn_reply_to_user_id(){  
    105.         return this.in_reply_to_user_id;  
    106.     }  
    107.     public void setIn_reply_to_user_id(String in_reply_to_user_id){  
    108.         this.in_reply_to_user_id = in_reply_to_user_id;  
    109.     }  
    110.     public String getIn_reply_to_user_id_str(){  
    111.         return this.in_reply_to_user_id_str;  
    112.     }  
    113.     public void setIn_reply_to_user_id_str(String in_reply_to_user_id_str){  
    114.         this.in_reply_to_user_id_str = in_reply_to_user_id_str;  
    115.     }  
    116.     public String getLang(){  
    117.         return this.lang;  
    118.     }  
    119.     public void setLang(String lang){  
    120.         this.lang = lang;  
    121.     }  
    122.     public boolean getPossibly_sensitive(){  
    123.         return this.possibly_sensitive;  
    124.     }  
    125.     public void setPossibly_sensitive(boolean possibly_sensitive){  
    126.         this.possibly_sensitive = possibly_sensitive;  
    127.     }  
    128.     public Number getRetweet_count(){  
    129.         return this.retweet_count;  
    130.     }  
    131.     public void setRetweet_count(Number retweet_count){  
    132.         this.retweet_count = retweet_count;  
    133.     }  
    134.     public boolean getRetweeted(){  
    135.         return this.retweeted;  
    136.     }  
    137.     public void setRetweeted(boolean retweeted){  
    138.         this.retweeted = retweeted;  
    139.     }  
    140.     public Retweeted_status getRetweeted_status(){  
    141.         return this.retweeted_status;  
    142.     }  
    143.     public void setRetweeted_status(Retweeted_status retweeted_status){  
    144.         this.retweeted_status = retweeted_status;  
    145.     }  
    146.     public String getSource(){  
    147.         return this.source;  
    148.     }  
    149.     public void setSource(String source){  
    150.         this.source = source;  
    151.     }  
    152.     public String getText(){  
    153.         return this.text;  
    154.     }  
    155.     public void setText(String text){  
    156.         this.text = text;  
    157.     }  
    158.     public boolean getTruncated(){  
    159.         return this.truncated;  
    160.     }  
    161.     public void setTruncated(boolean truncated){  
    162.         this.truncated = truncated;  
    163.     }  
    164.     public User getUser(){  
    165.         return this.user;  
    166.     }  
    167.     public void setUser(User user){  
    168.         this.user = user;  
    169.     }  
    170. }  


    整个过程显然很耗时间,而且还容易出现键入错误或数据类型匹配错误。

     

    一、自动生成Java存根Stub

    在线网站:http://jsongen.byingtondesign.com/

    它提供了JSON解析并对JSON数据结构进行建模,生成Java类的功能。你可以自定义包名,输出的内容是一个ZIP文件,里面根据包名路径,包含生成的Java实体类。


    你可以把得到的Java类文件放入到你的项目中,以便对JSON访问反序列化/序列化时使用。


    二、注意事项

    此工具能节省不少时间,然而,它不是一劳永逸的解决方案。

    JSON数据的一个显著缺点是其集合或属性的数据类型并不能通过程序100%精准的判断,这是因为数据的展现是宽松的。比如,一个整数值可以被表示为“1”或者1。而JSON Gen工具并不能确定“1”是整数还是字符串,因此你最终会得到大量的字符串类型的属性。所以,需要你手动地去检查每一个生成的Java类,看所有的私有属性的数据类型是否正确。

    此工具另一个潜在的问题是它在运行时只能关注对象,如果API响应变化,生成的Java文件或许会丢失部分元素。

    三、节省时间

    除开JSON Gen工具的不足,它实际上能节省你大量的开发时间,也会帮助你减少错误,不错的工具。

  • 相关阅读:
    C语言I博客作业04
    C语言I博客作业03
    C语言1博客作业02
    作业1
    C语言||作业01
    C语言寒假大作战04
    C语言寒假大作战03
    C语言寒假大作战02
    C语言寒假大作战01
    笔记本
  • 原文地址:https://www.cnblogs.com/ycpanda/p/3637155.html
Copyright © 2011-2022 走看看