使用库org.json 和 Gson 解析 JSON格式字符串

前端之家收集整理的这篇文章主要介绍了使用库org.json 和 Gson 解析 JSON格式字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
org.json库为JSON创始人编写的解析JSON的java库,Gson为Google为我们提供的解析JSON格式数据的库。有关库内容的的下载参见:http://www.json.org

以下是两个简单的测试程序


使用Gson库:

public class User {
	
	String username;
	String password;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}
//使用Google Gson库
		User user = new User();
		user.setUsername("shexinwei");
		user.setPassword("123456");
		
		Gson gson = new Gson();
		String json = gson.toJson(user);
		
		User user2 = gson.fromJson(json,User.class);
		System.out.println(json);
		System.out.println("username: "+user2.getUsername());
		System.out.println("password: "+user2.getPassword());


---------------------------------------------------------------------------------------------------------------------------------------------------------------------

使用org.json库

/*
{"weatherinfo":
			{"city":"上海","city_en":"shanghai","date_y":"2010年5月31日","date":"庚寅年四月十八","week":"星期一","fchh":"08","cityid":"101020100","temp1":"27℃~18℃","temp2":"26℃~18℃","temp3":"27℃~19℃","temp4":"27℃~20℃","temp5":"25℃~20℃","tempF1":"80.6℉~64.4℉","tempF2":"78.8℉~64.4℉","tempF3":"80.6℉~66.2℉","tempF4":"80.6℉~68℉","tempF5":"77℉~68℉","weather1":"多云","weather2":"多云","weather3":"晴转多云","weather4":"多云","weather5":"阴","img1":"1","img2":"99","img3":"1","img4":"99","img5":"0","img6":"1","img7":"1","img8":"99","img9":"2","img10":"99","img_single":"1","img_title1":"多云","img_title2":"多云","img_title3":"多云","img_title4":"多云","img_title5":"晴","img_title6":"多云","img_title7":"多云","img_title8":"多云","img_title9":"阴","img_title10":"阴","img_title_single":"多云","wind1":"东风3-4级","wind2":"东风3-4级","wind3":"东风3-4级","wind4":"东南风4-5级","wind5":"东南风转东风4-5级","fx1":"东风","fx2":"东风","fl1":"3-4级","fl2":"3-4级","fl3":"3-4级","fl4":"4-5级","fl5":"4-5级","index":"暂缺","index_d":"暂缺","index48":"暂缺","index48_d":"暂缺","index_uv":"弱","index48_uv":"弱","index_xc":"适宜","index_tr":"很适宜","index_co":"较舒适","st1":"26","st2":"17","st3":"25","st4":"17","st5":"25","st6":"18"
			}
} 
*/

		//使用org.json库
		String json2 = new String("{\"weatherinfo\":{\"city\":\"上海\",\"city_en\":\"shanghai\",\"date_y\":\"2010年5月31日\",\"date\":\"庚寅年四月十八\",\"week\":\"星期一\",\"fchh\":\"08\",\"cityid\":\"101020100\",\"temp1\":\"27℃~18℃\",\"temp2\":\"26℃~18℃\",\"temp3\":\"27℃~19℃\",\"temp4\":\"27℃~20℃\",\"temp5\":\"25℃~20℃\",\"tempF1\":\"80.6℉~64.4℉\",\"tempF2\":\"78.8℉~64.4℉\",\"tempF3\":\"80.6℉~66.2℉\",\"tempF4\":\"80.6℉~68℉\",\"tempF5\":\"77℉~68℉\",\"weather1\":\"多云\",\"weather2\":\"多云\",\"weather3\":\"晴转多云\",\"weather4\":\"多云\",\"weather5\":\"阴\",\"img1\":\"1\",\"img2\":\"99\",\"img3\":\"1\",\"img4\":\"99\",\"img5\":\"0\",\"img6\":\"1\",\"img7\":\"1\",\"img8\":\"99\",\"img9\":\"2\",\"img10\":\"99\",\"img_single\":\"1\",\"img_title1\":\"多云\",\"img_title2\":\"多云\",\"img_title3\":\"多云\",\"img_title4\":\"多云\",\"img_title5\":\"晴\",\"img_title6\":\"多云\",\"img_title7\":\"多云\",\"img_title8\":\"多云\",\"img_title9\":\"阴\",\"img_title10\":\"阴\",\"img_title_single\":\"多云\",\"wind1\":\"东风3-4级\",\"wind2\":\"东风3-4级\",\"wind3\":\"东风3-4级\",\"wind4\":\"东南风4-5级\",\"wind5\":\"东南风转东风4-5级\",\"fx1\":\"东风\",\"fx2\":\"东风\",\"fl1\":\"3-4级\",\"fl2\":\"3-4级\",\"fl3\":\"3-4级\",\"fl4\":\"4-5级\",\"fl5\":\"4-5级\",\"index\":\"暂缺\",\"index_d\":\"暂缺\",\"index48\":\"暂缺\",\"index48_d\":\"暂缺\",\"index_uv\":\"弱\",\"index48_uv\":\"弱\",\"index_xc\":\"适宜\",\"index_tr\":\"很适宜\",\"index_co\":\"较舒适\",\"st1\":\"26\",\"st2\":\"17\",\"st3\":\"25\",\"st4\":\"17\",\"st5\":\"25\",\"st6\":\"18\"}} ");
		//构建最外层JSONObject
		JSONObject jsonObject = new JSONObject(json2);
		//构建JSONObject对象weather
		JSONObject weatherInfo = jsonObject.getJSONObject("weatherinfo");
		System.out.println("城市:"+weatherInfo.getString("city"));
		System.out.println("en: "+weatherInfo.getString("city_en"));



使用org.json解析JSONArray数据:

/*
//xml格式数据
<users>
	<user gender = "male">
		<username>shexinwei</username>
		<password>1234</password>
	</user>
	<user gender = "female">
		<username>xidian</username>
		<password>university</password>
	</user>
</users>


//同义的JSON格式数据
{
   "users":
		[
			{
			   "gernder":"male","username":"shexinwei","password":"1234"
			},{
			   "gender":"female","username":"xidian","password":"university"
			}
		]
}
*/
//org.json库中使用数组JSONArray
		String json3 = new String("{\"users\":[{ \"gernder\":\"male\",\"username\":\"shexinwei\",\"password\":\"1234\"},{\"gender\":\"female\",\"username\":\"xidian\",\"password\":\"university\"}]}");
		//构建整个JSON对象
		JSONObject jsonObject2 = new JSONObject(json3);
		//获取键值users对应的数组JSONArray
		JSONArray users = jsonObject2.getJSONArray("users");
		//获取数组中的第一个元素
		JSONObject user1 = (JSONObject) users.get(0);
		System.out.println("username: "+user1.getString("username"));
        System.out.println("password: "+user1.getString("password"));

猜你在找的Json相关文章