--------------------转载请注明:http://blog.csdn.net/feiduclear_up/article/details/42499409


概要

怎么才能快速的开发出带json的android应用。自己定义json对应的具体java beans,用Android自带的Json库解析json是一件很繁琐的事情。所以在这里,我引入一个工具和一个库。

  • Jsonschema2pojo:可以更具json自动生成出相应的javaclasses(http://code.google.com/p/jsonschema2pojo/)
  • Jackson:可以将java对象转换成json,也可以将json转换成java对象。(http://jackson.codehaus.org/)


Jsonschema2pojo

我们使用命令行模式来从json生成javaclasses

  • 从下面的地址下载最新的版本:

https://github.com/joelittlejohn/jsonschema2pojo/releases

为了防止下不了,也可以使用自己的下载地址,国内的地址毕竟快,而且稳定。附上地址:

http://download.csdn.net/detail/feidu804677682/8520253


  • 解压下载的版本,你会看见很多的jsonschema2pojo jar文件,一个lib文件夹和两个脚本文件。
  • 将你要使用的json文件放到解压目录下(可以到http://www.json-schema.org/address下载Json例子):
  • 然后在命令行下输入:

./jsonschema2pojo --source address --target java-gen

你可以输入—help选项查看其它参数:

./jsonschema2pojo –help

对有的非标准的Json文件,你需要加入-T参数

./jsonschema2pojo --source address --target java-gen -T JSON -a NONE

  • 现在你就可以在java-gen文件夹下面找到生成的java class文件了。
  • 在Android项目中使用这些class文件,你需要删除其中的@Generated("com.googlecode.jsonschema2pojo")
  • 注意:address 是你需要转换的json数据。


举例说明:

天气预报的json数据 test.json

{    "weatherinfo": {        "city": "珠海",        "cityid": "101280701",        "temp1": "14℃",        "temp2": "19℃",        "weather": "多云",        "img1": "n1.gif",        "img2": "d1.gif",        "ptime": "18:00"    }}

经过转换之后生成如下实体类 自动生成Weatherinfo.java java文件,都不需要我自己命名,这个类直接拿过来使用会有些错误,将错误去除就可以使用了。

import java.util.HashMap;import java.util.Map;import javax.annotation.Generated;import org.apache.commons.lang.builder.EqualsBuilder;import org.apache.commons.lang.builder.HashCodeBuilder;import org.apache.commons.lang.builder.ToStringBuilder;@Generated("org.jsonschema2pojo")public class Weatherinfo {    private String city;    private String cityid;    private String temp1;    private String temp2;    private String weather;    private String img1;    private String img2;    private String ptime;    private Map<String, Object> additionalProperties = new HashMap<String, Object>();    /**     *      * @return     *     The city     */    public String getCity() {        return city;    }    /**     *      * @param city     *     The city     */    public void setCity(String city) {        this.city = city;    }    /**     *      * @return     *     The cityid     */    public String getCityid() {        return cityid;    }    /**     *      * @param cityid     *     The cityid     */    public void setCityid(String cityid) {        this.cityid = cityid;    }    /**     *      * @return     *     The temp1     */    public String getTemp1() {        return temp1;    }    /**     *      * @param temp1     *     The temp1     */    public void setTemp1(String temp1) {        this.temp1 = temp1;    }    /**     *      * @return     *     The temp2     */    public String getTemp2() {        return temp2;    }    /**     *      * @param temp2     *     The temp2     */    public void setTemp2(String temp2) {        this.temp2 = temp2;    }    /**     *      * @return     *     The weather     */    public String getWeather() {        return weather;    }    /**     *      * @param weather     *     The weather     */    public void setWeather(String weather) {        this.weather = weather;    }    /**     *      * @return     *     The img1     */    public String getImg1() {        return img1;    }    /**     *      * @param img1     *     The img1     */    public void setImg1(String img1) {        this.img1 = img1;    }    /**     *      * @return     *     The img2     */    public String getImg2() {        return img2;    }    /**     *      * @param img2     *     The img2     */    public void setImg2(String img2) {        this.img2 = img2;    }    /**     *      * @return     *     The ptime     */    public String getPtime() {        return ptime;    }    /**     *      * @param ptime     *     The ptime     */    public void setPtime(String ptime) {        this.ptime = ptime;    }    @Override    public String toString() {        return ToStringBuilder.reflectionToString(this);    }    public Map<String, Object> getAdditionalProperties() {        return this.additionalProperties;    }    public void setAdditionalProperty(String name, Object value) {        this.additionalProperties.put(name, value);    }    @Override    public int hashCode() {        return new HashCodeBuilder().append(city).append(cityid).append(temp1).append(temp2).append(weather).append(img1).append(img2).append(ptime).append(additionalProperties).toHashCode();    }    @Override    public boolean equals(Object other) {        if (other == this) {            return true;        }        if ((other instanceof Weatherinfo) == false) {            return false;        }        Weatherinfo rhs = ((Weatherinfo) other);        return new EqualsBuilder().append(city, rhs.city).append(cityid, rhs.cityid).append(temp1, rhs.temp1).append(temp2, rhs.temp2).append(weather, rhs.weather).append(img1, rhs.img1).append(img2, rhs.img2).append(ptime, rhs.ptime).append(additionalProperties, rhs.additionalProperties).isEquals();    }}



更多相关文章

  1. android camera以时间来命名照片文件
  2. android编辑布局文件时非要降低api level或者更改主题才能显示布
  3. Android学习笔记-Android非布局activity中布局文件及控件加载方
  4. 给Android增加分享库.so文件
  5. Android的log保存到文件上查看
  6. Android应用程序中Manifest.java文件的介绍
  7. Android多文件断点续传(三)——实现文件断点续传
  8. Android获取本机IP地址(不是localhost)和MAC的方法

随机推荐

  1. android调用系统相机返回null的bug
  2. Android: 一个两点触控的案例
  3. android 如何遍历Cursor
  4. AS配置NDK外部工具
  5. Android实现JSON对象获取登录新浪微博
  6. 原创:Android APN 设置 之 cmnet 改 cmwap
  7. android studio 代码混淆
  8. Android Service 中 onStartCommand()函
  9. Android触摸事件传递机制学习笔记
  10. android使用sharedPreferences()方法读写