把ArrayList轉化為json物件
阿新 • • 發佈:2019-01-06
import java.util.ArrayList; //主類 public class readHotel { public static void main(String[] args){ // readHotelInformation(); // distance_hotel(113.32344,23.11779); ArrayList<jsonHotel> hotel=new ArrayList<jsonHotel>(); hotel.add(new jsonHotel("zhangsan","1","zhangsan","1","zhangsan","1","zhangsan","1","zhangsan","1","zhangsan")); hotel.add(new jsonHotel("zhangsadsfn","1","zhaaaaangsan","1sdfsd","zhaadfngsan","1","zhansdfgsan","1","zhangsan","1","zhangsan")); JSONArray result = JSONArray.fromObject(hotel); System.out.println(result.toString(4)); // System.out.println(hotelMessage[0][1]+" "+hotelMessage[0][2]); } }
/** * 存放hotel資訊,用來之後變成json格式傳遞的 * @author llq * */ public class jsonHotel { private String hotelName; private String CommentAddress; private String hotelWeb; private String hotelAddress; private String hotelLongitude; private String hotelLatitude; private String commentNumber; private String hotelScore; private String hotelPicture; private String hotelPrice; private String distance; public jsonHotel(String hotelName, String CommentAddress,String hotelWeb,String hotelAddress, String hotelLongitude,String hotelLatitude,String commentNumber,String hotelScore, String hotelPicture,String hotelPrice,String distance) { this.hotelName=hotelName; this.CommentAddress=CommentAddress; this.hotelWeb=hotelWeb; this.hotelAddress=hotelAddress; this.hotelLongitude=hotelLongitude; this.hotelLatitude=hotelLatitude; this.commentNumber=commentNumber; this.hotelScore=hotelScore; this.hotelPicture=hotelPicture; this.hotelPrice=hotelPrice; this.distance=distance; } public String getHotelName(){ return hotelName; } public String getCommentAddress(){ return CommentAddress; } public String getHotelWeb(){ return hotelWeb; } public String gethotelAddress(){ return hotelAddress; } public String getHotelLongitude(){ return hotelLongitude; } public String getHotelLatitude(){ return hotelLatitude; } public String getCommentNumber(){ return commentNumber; } public String getHotelScore(){ return hotelScore; } public String getHotelPicture(){ return hotelPicture; } public String getHotelPrice(){ return hotelPrice; } public String getDistance(){ return distance; } }
最後輸出: