Android Json uses jsonschema2pojo to generate a. java File

Source: Internet
Author: User

Android Json uses jsonschema2pojo to generate a. java File
 
Summary

How to quickly develop android applications with json. Defining specific java beans corresponding to json by yourself. parsing Json with the json library that comes with Android is very cumbersome. So here I introduce a tool and a library.

Jsonschema2pojo: More json can automatically generate the corresponding javaclasses (http://code.google.com/p/jsonschema2pojo) Jackson: You can convert a java object to json or a java object. (Www.bkjia.com)

 

Jsonschema2pojo

We use the command line mode to generate javaclasses from json

 

 

Decompress the downloaded version and you will see many jsonschema2pojo jar files, one lib folder and two script files. Put the json file you want to use in the decompressed directory (you can download the jsonexample at http://www.json-schema.org/address): then enter:

Jsonschema2pojo -- source address -- target java-gen

You can enter the-help option to view other parameters:

Jsonschema2pojo-help

For some non-standard Json files, you need to add the-T parameter

Jsonschema2pojo -- source address -- target java-gen-t json-a NONE

Now you can find the generated java class file under the java-gen folder. To use these class files in an Android project, you need to delete @ Generated (com. googlecode. jsonschema2pojo). Note: address is the json data to be converted.

 

Example:

Json data test. json of weather forecast

 

{Weatherinfo: {city: Zhuhai, cityid: 101280701, temp1: 14 ℃, temp2: 19 ℃, weather: Cloudy, img1: n1.gif, img2: d1.gif, ptime }}

 

After the conversion, the following entity classes are generated to automatically generate the Weatherinfo. java file, which does not need to be named by myself. If this class is taken directly, there will be some errors. You can simply remove the errors and use them.

 

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
 
   additionalProperties = new HashMap
  
   ();    /**     *      * @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
   
     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();    }}
   
  
 


 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.