package io.renren.modules.us.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author gaoxipeng
* @Date 2018/9/10 17:08
* @Description 解析从高德返回的复杂json,以更友好的json格式返回客户端。查询下路接口调用的方法
*/
public class BusRouteUtils {
private static final int SECOND=3600;
private static Logger logger = LoggerFactory.getLogger(BusRouteUtils.class);
public static String busTaints(String response,String strategy){
String s ="";
try {
JSONObject jsonObject = JSONObject.parseObject(response);
JSONObject route = jsonObject.getJSONObject("route");
JSONArray transits = route.getJSONArray("transits");
for (int i = 0; i <transits.size() ; i++) {
JSONObject info = transits.getJSONObject(i);
//高德返回walking_distance的value值为空的时候是数组形式,这里换乘字符串返回客服端
if(info.getString("walking_distance").length()<=2){
info.put("walking_distance", "");
}
info.put("programme", "");
String cost = info.getString("cost");
String s1;
if(cost.length()>2) {
int i2 = cost.indexOf(".");
String cost1 = cost.substring(0, i2);
StringBuffer stringBuffer3 = new StringBuffer();
stringBuffer3.append(cost1).append("元");
s1 = stringBuffer3.toString();
}else {
s1 ="";
}
String duration = info.getString("duration");
//将秒转化为分钟、小时的格式返回
info.put("duration", getTimes(duration));
//System.out.println("总时间:"+info.getString("duration"));
JSONArray segments = info.getJSONArray("segments");
//按条件查询去第出行方案,第一条即为最快或换乘少或步行少
JSONObject jsonObject3 = transits.getJSONObject(0);
if ("0".equals(strategy)) {
jsonObject3.put("programme", "最快");
}else if ("2".equals(strategy)){
jsonObject3.put("programme", "换乘少");
} else if ("3".equals(strategy)){
jsonObject3.put("programme", "步行少");
}
String departure_stop = ""; //在哪上车
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int j = 0; j < segments.size() ; j++) {
JSONObject jsonObject1 = segments.getJSONObject(j);
//以下是乘坐地铁的相关字段,不必要返回前端
jsonObject1.remove("exit");
jsonObject1.remove("taxi");
jsonObject1.remove("railway");
jsonObject1.remove("entrance");
//防止walking数据为空报异常
if(jsonObject1.getString("walking").length()>2) {
JSONObject walking = jsonObject1.getJSONObject("walking");
if (walking.size() != 0) {
String duration2 = walking.getString("duration");
walking.put("duration", getTimes(duration2));
String duration3 = walking.getString("duration");
//System.out.println("步行时间:" + duration3);
}
}
JSONObject bus = jsonObject1.getJSONObject("bus");
if(bus.getString("buslines").length()>2) {
JSONArray buslines = bus.getJSONArray("buslines");
Map<String, Object> map = new HashMap<>();
StringBuffer stringBuffer = new StringBuffer();
for (int k = 0; k < buslines.size(); k++) {
JSONObject jsonOne = buslines.getJSONObject(k);
if (jsonOne.size() != 0) {
String duration3 = jsonOne.getString("duration");
jsonOne.put("duration", getTimes(duration3));
String duration4 = jsonOne.getString("duration");
//System.out.println("坐公交时间:" + duration4);
//在transits层添加具体换乘的所有公交线路
String name = jsonOne.getString("name");
int i1 = name.indexOf("(");
String substring = name.substring(0,i1);
stringBuffer.append(substring).append("/ ");
}
//将乘车起始站点departure_stop返回到transits层以便前端更好的读取
JSONObject One = segments.getJSONObject(0);
JSONObject bus1 = One.getJSONObject("bus");
JSONArray buslines1 = bus1.getJSONArray("buslines");
for (int l = 0; l < buslines1.size() ; l++) {
JSONObject jsonObject2 = buslines1.getJSONObject(0);
JSONObject departure_stop1 = jsonObject2.getJSONObject("departure_stop");
departure_stop = departure_stop1.getString("name");
}
}
stringBuffer.deleteCharAt(stringBuffer.length() - 2);
map.put("line", stringBuffer.toString().trim());
list.add(map);
}
}
//将list转化为jsonArray拼接到原有json中
net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(list);
//拼接起乘站点到json
info.put("departure_stop", departure_stop);
//拼接换乘路线到json
info.put("bus_line", jsonArray);
//拼接具体乘车路线id返回客户端,以便线路详情接口针对性的查询
info.put("id", i + "");
//eg:4.0,改成4元
info.put("cost", s1);
}
///线路查询去除多余参数segments
JSONArray transits1 = route.getJSONArray("transits");
for (int i = 0; i <transits1.size() ; i++) {
JSONObject jsonObject1 = transits1.getJSONObject(i);
jsonObject1.remove("distance");
jsonObject1.remove("missed");
jsonObject1.remove("nightflag");
}
s = jsonObject.toString();
//生产环境此日志建议注释
//logger.info("格式化后的json:"+s);
}catch (RuntimeException e) {
e.printStackTrace();
}
return s;
}
/**
* @author gaoxipeng
* @Date 2018/9/14 13:08
* @Description 解析从高德返回的复杂json,以更友好的json格式返回客户端 线路详情接口调用的方法
*/
public static String busTaintDetils(String s, String id) {
JSONObject jsonObject = JSONObject.parseObject(s);
JSONObject route = jsonObject.getJSONObject("route");
JSONArray transits = route.getJSONArray("tran
java调用高德公交api(java源码)
需积分: 27 7 浏览量
2018-10-18
13:29:41
上传
评论 3
收藏 7KB RAR 举报
gaoxp666
- 粉丝: 55
- 资源: 6