![]()
package json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.google.gson.Gson;
public class JsonExample {
/**
* 使用Gson可以直接將實體類轉化爲json字符串
* @return
*/
public static String createJsonByGson() {
HotelResponse hotelResponse = new HotelResponse();
List<Hotel> hotelList = new ArrayList<Hotel>();
Hotel hotel1 = new Hotel();
Hotel hotel2 = new Hotel();
Hotel hotel3 = new Hotel();
Hotel hotel4 = new Hotel();
hotel1.setHotelName("七天連鎖酒店");
hotel1.setAddress("長安街001號");
hotel2.setHotelName("如家連鎖酒店");
hotel2.setAddress("長安街002號");
hotel3.setHotelName("漢庭連鎖酒店");
hotel3.setAddress("長安街003號");
hotel4.setHotelName("維多利亞連鎖酒店");
hotel4.setAddress("長安街004號");
hotelList.add(hotel1);
hotelList.add(hotel2);
hotelList.add(hotel3);
hotelList.add(hotel4);
hotelResponse.setTotalRecords(999);
hotelResponse.setPagesize(4);
hotelResponse.setCurrpage(1);
hotelResponse.setHotelList(hotelList);
Gson gson = new Gson();
String jsonStr = gson.toJson(hotelResponse);
System.out.println("jsonStr = " + jsonStr);
return jsonStr;
}
/**
* 使用Gson解析json串
*/
public static void praseJsonByGson() {
String jsonStr = createJsonByGson();
Gson gson = new Gson();
HotelResponse hotelResponse = gson.fromJson(jsonStr, HotelResponse.class);
int totalRecords = hotelResponse.getTotalRecords();
int pagesize = hotelResponse.getPagesize();
int currpage = hotelResponse.getCurrpage();
List<Hotel> hotelList = hotelResponse.getHotelList();
System.out.println("totalRecords = " + totalRecords);
}
/**
* 使用JSONObject可以直接將Map轉化爲json字符串
* @return
*/
public static String createJsonByJSONObject() {
JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
Map<String, Object> map1 = new HashMap<String, Object>();
Map<String, Object> map2 = new HashMap<String, Object>();
Map<String, Object> map3 = new HashMap<String, Object>();
Map<String, Object> map4 = new HashMap<String, Object>();
map1.put("hotelName", "七天連鎖酒店");
map1.put("address", "長安街001號");
map2.put("hotelName", "如家連鎖酒店");
map2.put("address", "長安街002號");
map3.put("hotelName", "漢庭連鎖酒店");
map3.put("address", "長安街003號");
map4.put("hotelName", "維多利亞連鎖酒店");
map4.put("address", "長安街004號");
ja.add(map1);
ja.add(map2);
ja.add(map3);
ja.add(map4);
jo.put("totalRecords", 999);
jo.put("pagesize", 4);
jo.put("currpage", 1);
jo.put("hotelList", ja);
String jsonStr = jo.toString();
System.out.println("jsonStr = " + jsonStr);
return jsonStr;
}
/**
* 使用JSONObject解析json串
*/
public static void praseJsonByJSONObject() {
String jsonStr = createJsonByJSONObject();
JSONObject jo = JSONObject.fromObject(jsonStr);
int totalRecords = jo.getInt("totalRecords");
int pagesize = jo.getInt("pagesize");
int currpage = jo.getInt("currpage");
JSONArray ja = jo.getJSONArray("hotelList");
for(int i=0; i<ja.size(); i++) {
String hotelName = ((JSONObject)ja.get(i)).getString("hotelName");
System.out.println("hotelName = " + hotelName);
}
System.out.println("totalRecords = " + totalRecords);
}
public static void main(String[] args) {
praseJsonByJSONObject();
}
}
package json;
import java.util.List;
public class HotelResponse {
private int totalRecords;
private int pagesize;
private int currpage;
private List<Hotel> hotelList;
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public int getCurrpage() {
return currpage;
}
public void setCurrpage(int currpage) {
this.currpage = currpage;
}
public List<Hotel> getHotelList() {
return hotelList;
}
public void setHotelList(List<Hotel> hotelList) {
this.hotelList = hotelList;
}
}
package json;
public class Hotel {
private String hotelName;
private String address;
public String getHotelName() {
return hotelName;
}
public void setHotelName(String hotelName) {
this.hotelName = hotelName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}