Android Json 使用jsonschema2pojo產生.java檔案檔案,android產生json

來源:互聯網
上載者:User

Android Json 使用jsonschema2pojo產生.java檔案檔案,android產生json

--------------------轉載請註明: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/8338311


  • 解壓下載的版本,你會看見很多的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();    }}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.