使用Gson转化天气预报Json

国家气象局提供的天气预报接口主要有三个,分别是:

http://www.weather.com.cn/data/sk/101010100.html

http://www.weather.com.cn/data/cityinfo/101010100.html

http://m.weather.com.cn/data/101010100.html

访问http://www.weather.com.cn/data/sk/101010100.html得到如下天气预报的Json

{
    "weatherinfo": {
        "city": "北京", 
        "cityid": "101010100", 
        "temp": "9", 
        "WD": "西南风", 
        "WS": "2级", 
        "SD": "24%", 
        "WSE": "2", 
        "time": "10:30", 
        "isRadar": "1", 
        "Radar": "JC_RADAR_AZ9010_JB", 
        "njd": "暂无实况", 
        "qy": "1015"
    }
}

北京地区的预报区域代码为:

101010100=北京
101010200=海淀
101010300=朝阳
101010400=顺义
101010500=怀柔
101010600=通州
101010700=昌平
101010800=延庆
101010900=丰台
101011000=石景山
101011100=大兴
101011200=房山
101011300=密云
101011400=门头沟
101011500=平谷
101011600=八达岭
101011700=佛爷顶
101011800=汤河口
101011900=密云上甸子
101012000=斋堂
101012100=霞云岭

我们先定义天气信息的实体类Weatherinfo 

public class Weatherinfo {

	private String city;
	private String cityid;
	private String temp;
	private String WD;
	private String WS;
	private String SD;
	private String WSE;
	private String time;
	private String isRadar;
	private String njd;
	private String qy;

	public Weatherinfo(String city, String cityid, String temp, String wD,
			String wS, String sD, String wSE, String time, String isRadar,
			String njd, String qy) {
		super();
		this.city = city;
		this.cityid = cityid;
		this.temp = temp;
		WD = wD;
		WS = wS;
		SD = sD;
		WSE = wSE;
		this.time = time;
		this.isRadar = isRadar;
		this.njd = njd;
		this.qy = qy;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCityid() {
		return cityid;
	}

	public void setCityid(String cityid) {
		this.cityid = cityid;
	}

	public String getTemp() {
		return temp;
	}

	public void setTemp(String temp) {
		this.temp = temp;
	}

	public String getWD() {
		return WD;
	}

	public void setWD(String wD) {
		WD = wD;
	}

	public String getWS() {
		return WS;
	}

	public void setWS(String wS) {
		WS = wS;
	}

	public String getSD() {
		return SD;
	}

	public void setSD(String sD) {
		SD = sD;
	}

	public String getWSE() {
		return WSE;
	}

	public void setWSE(String wSE) {
		WSE = wSE;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getIsRadar() {
		return isRadar;
	}

	public void setIsRadar(String isRadar) {
		this.isRadar = isRadar;
	}

	public String getNjd() {
		return njd;
	}

	public void setNjd(String njd) {
		this.njd = njd;
	}

	public String getQy() {
		return qy;
	}

	public void setQy(String qy) {
		this.qy = qy;
	}
}

把Weatherinfo类加到Weather

public class Weather {

	private Weatherinfo weatherinfo;
	
	public Weatherinfo getWeatherinfo() {
		return weatherinfo;
	}

	public void setWeatherinfo(Weatherinfo weatherinfo) {
		this.weatherinfo = weatherinfo;
	}
}

使用Gson把json字符串转化为实体

String jsonString = HttpResonse;  //返回的天气预报字符串
Gson gson = new Gson();
Weather weather = gson.fromJson(jsonString, Weather.Class); 

System.out.print(weather.getWeatherinfo().getCity());



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现 ResultSet 与 JSON 相互转化: 1. 将 ResultSet 转化为 List<Map<String, Object>> 对象。 2. 使用 Gson 将 List<Map<String, Object>> 对象转化JSON 格式的字符串。 3. 使用 GsonJSON 格式的字符串转化为 List<Map<String, Object>> 对象。 4. 将 List<Map<String, Object>> 对象转化为 ResultSet 对象。 以下是 Java 代码示例: ```java import com.google.gson.Gson; import java.sql.*; import java.util.*; public class ResultSetToJson { public static void main(String[] args) throws SQLException { // 获取数据库连接 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false", "root", "password"); // 执行 SQL 查询语句 Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM users"); // 将 ResultSet 转化为 List<Map<String, Object>> 对象 List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>(); ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); while (rs.next()) { Map<String, Object> map = new HashMap<String, Object>(); for (int i = 1; i <= columnCount; i++) { String columnName = metaData.getColumnLabel(i); Object value = rs.getObject(columnName); map.put(columnName, value); } resultList.add(map); } // 使用 Gson 将 List<Map<String, Object>> 对象转化JSON 格式的字符串 Gson gson = new Gson(); String json = gson.toJson(resultList); System.out.println(json); // 使用 GsonJSON 格式的字符串转化为 List<Map<String, Object>> 对象 List<Map<String, Object>> resultList2 = gson.fromJson(json, List.class); // 将 List<Map<String, Object>> 对象转化为 ResultSet 对象 ResultSetMetaData metaData2 = rs.getMetaData(); ResultSet rs2 = new ResultSetImpl(resultList2, metaData2); while (rs2.next()) { System.out.println(rs2.getInt("id") + " " + rs2.getString("name")); } // 关闭 ResultSet、Statement 和 Connection 对象 rs.close(); stmt.close(); conn.close(); } } class ResultSetImpl implements ResultSet { private List<Map<String, Object>> resultList; private ResultSetMetaData metaData; private int rowIndex = -1; public ResultSetImpl(List<Map<String, Object>> resultList, ResultSetMetaData metaData) { this.resultList = resultList; this.metaData = metaData; } @Override public boolean next() throws SQLException { rowIndex++; return rowIndex < resultList.size(); } @Override public String getString(String columnLabel) throws SQLException { Object value = resultList.get(rowIndex).get(columnLabel); return value == null ? null : value.toString(); } // 实现 ResultSet 接口中的其他方法 } ``` 需要注意的是,由于 ResultSet 接口中的方法较多,以上代码仅实现了部分方法,具体实现可根据需求进行扩展。另外,由于 ResultSet 接口是只读的,因此在实现 ResultSet 接口时不能包含更新、插入或删除等方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值