Android Json uses Jsonschema2pojo to generate. java File files

Source: Internet
Author: User

-------------------- Reprint Please specify:http://blog.csdn.net/feiduclear_up/article/details/42499409


Profile

How to quickly develop an Android app with JSON. It is a chore to define JSON-specific Java beans and parse JSON with the JSON library that comes with Android. So here I introduce a tool and a library.

    • Jsonschema2pojo: Can be more JSON automatically generated corresponding javaclasses (http://code.google.com/p/jsonschema2pojo/)
    • Jackson: You can convert a Java object to JSON, or you can convert the JSON to a Java object. (http://jackson.codehaus.org/)


Jsonschema2pojo

We use command-line mode to generate javaclasses from JSON

    • Download the latest version from the address below:

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

In order to prevent the next, can also use their own, the domestic address is fast, and stable. Attached address:

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


    • Unzip the downloaded version and you will see a lot of Jsonschema2pojo jar files, a Lib folder and two script files.
    • Put the JSON file you want to use in the Extract directory (you can download the JSON example to http://www.json-schema.org/address):
    • Then, at the command line, enter:

Jsonschema2pojo--source Address--target Java-gen

You can enter the-HELP option to view additional parameters:

Jsonschema2pojo–help

For some non-standard JSON files, you need to include 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 the @generated ("Com.googlecode.jsonschema2pojo")
    • Note: Address is the JSON data you need to convert.


To illustrate:

JSON data Test.json for weather forecasts

{"    Weatherinfo": {        "city": "Zhuhai",        "Cityid": "101280701",        "Temp1": "14 ℃",        "Temp2": "19 ℃",        " Weather ":" Cloudy ","        img1 ":" N1.gif ",        " Img2 ":" D1.gif ",        " Ptime ":" 18:00 "    }}

After the conversion to generate the following entity class automatically generated Weatherinfo.java Java files, do not need my own name, this class directly to use there will be some errors, error removal can be used.

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). A PPend (TEMP2). Append (Weather). Append (IMG1). Append (Img2). Append (Ptime). Append (additionalproperties). Tohashcode ();        } @Override public boolean equals (Object) {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 (); }}



Android Json uses Jsonschema2pojo to generate. java File files

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.