--------------------转载请注明:http://blog.csdn.net/feiduclear_up/article/details/42499409
怎么才能快速的开发出带json的android应用。自己定义json对应的具体java beans,用Android自带的Json库解析json是一件很繁琐的事情。所以在这里,我引入一个工具和一个库。
我们使用命令行模式来从json生成javaclasses
https://github.com/joelittlejohn/jsonschema2pojo/releases
为了防止下不了,也可以使用自己的下载地址,国内的地址毕竟快,而且稳定。附上地址:
http://download.csdn.net/detail/feidu804677682/8338311
jsonschema2pojo --source address --target java-gen
你可以输入—help选项查看其它参数:
jsonschema2pojo –help
对有的非标准的Json文件,你需要加入-T参数
jsonschema2pojo --source address --target java-gen -T JSON -a NONE
举例说明:
天气预报的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();
}
}
Android Json 使用jsonschema2pojo生成.java文件文件
原文地址:http://blog.csdn.net/feiduclear_up/article/details/42499409