Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package step2;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import java.io.*;
- public class Task {
- /**
- * 使用fastjson解析数据
- * @param hotelResult 已经为你解析的所需json数据
- * @return
- */
- public List<Hotel> getHotle(String hotelResult){
- /********** Begin **********/
- List<Hotel> a = new ArrayList<Hotel>();
- JSONObject b = JSONObject.parseObject(hotelResult);
- List<Hotel> c = JSON.parseArray(b.getString("hotelPositionJSON"), Hotel.class);
- JSONArray hotelsPrice = b.getJSONArray("htllist");
- if (hotelsPrice != null && !hotelsPrice.isEmpty()) {
- for (int i = 0; i < c.size(); i++) {
- JSONObject priceObj = hotelsPrice.getJSONObject(i);
- if (priceObj != null && !priceObj.isEmpty()) {
- Hotel hotel = c.get(i);
- String hotelId = priceObj.getString("hotelid");
- double price = priceObj.getDoubleValue("amount");
- if (hotel.getId().equals(hotelId)) {
- hotel.setPrice(price);
- }
- }
- }
- }
- a.addAll(c);
- return a;
- /********** End **********/
- }
- /**
- * 由于携程网站经常更新,为了不影响测试,我们直接读取本地文件。
- * @return
- */
- public String getHotelListString(String cityId,String url){
- String hotelResult="";
- try {
- InputStream is = new FileInputStream(new File("src/step2/hotelResult.txt"));
- byte[] b=new byte[1024];
- int len=0;
- try {
- while((len=is.read(b))!=-1){
- String str=new String(b,0,len);
- hotelResult+=str;
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- return hotelResult;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement