<dependencies>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>2.0.26version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.24version>
dependency>
dependencies>
官网-> https://lbs.qq.com/dev/console/application/mine
官方文档: https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder
package com.jsxs.TentcentMap.inverseAddressResolution;
/**
* @Author Jsxs
* @Date 2023/4/4 19:39
* @PackageName:com.jsxs
* @ClassName: Test
* @Description: TODO 经纬度转地址
* @Version 1.0
*/
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.net.URL;
public class inverseAddressResolution {
/**
* 通过腾讯地图将经纬度转成详细地址
*
* @param lat 纬度
* @param lng 经度
* @param key 开发密钥(Key) 需自行申请
* @return
*/
public static JSONObject getLatAndLng(String lat, String lng, String key) throws Exception {
// 1. 腾讯地图密钥
String hsUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + lat + "," + lng + "&key=" + key + "&get_poi=1";
// 2. 设置URL
URL url = null;
// 3. 设置缓存输入流()
BufferedReader in = null;
// 4. 设置缓冲区
StringBuffer sb = new StringBuffer();
try {
// 5. 初始化URL对象
url = new URL(hsUrl);
// 6. 初始化初始化
in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
// 7.
String str = null;
// 8. 进行读取数据并写入数据(一行行读取)
while ((str = in.readLine()) != null) {
// 9. 添加到我们的字符串缓冲区
sb.append(str);
}
} catch (Exception ex) {
System.out.println("出现错误");
} finally {
try {
// 10. 关闭流
if (in != null) {
in.close();
}
} catch (IOException ex) {
}
}
// 1.这里是JSON字符串
String result = sb.toString();
// 2.JSON字符串转换为JSON对象
JSONObject object = JSONObject.parseObject(result);
// 3.通过JSON对象获取result这个对象属性
JSONObject result1 = (JSONObject) object.get("result");
// 4.然后再次获取address_component这个对象。
JSONObject address_component = (JSONObject) result1.get("address_component");
return address_component;
}
public static void main(String[] args) throws Exception {
System.out.println(getLatAndLng("43.828281", "125.29985", "你的密钥")); // 密钥⭐
}
}
package com.jsxs.TencentMapUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
/**
* @Author Jsxs
* @Date 2023/4/5 13:37
* @PackageName:com.jsxs.TencentMapUtils
* @ClassName: TencentMapUtils
* @Description: TODO
* @Version 1.0
*/
public class TencentMapUtils {
// 1. 工具类
public static StringBuffer getTencentMap(String keyUrl){
//请求的url
URL url = null;
//请求的输入流
BufferedReader in = null;
//输入流的缓冲
StringBuffer sb = new StringBuffer();
try {
url = new URL(keyUrl);
in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
String str = null;
//一行一行进行读入
while ((str = in.readLine()) != null) {
sb.append(str);
}
} catch (Exception ex) {
System.out.println("读取出现异常...");
} finally {
try {
if (in != null) {
in.close(); //关闭流
}
} catch (IOException ex) {
System.out.println("关闭资源出现异常");
}finally {
return sb;
}
}
}
}
官方文档: https://lbs.qq.com/service/webService/webServiceGuide/webServiceGeocoder
package com.jsxs.TentcentMap.addressResolution;
import com.alibaba.fastjson.JSONObject;
import com.jsxs.TencentMapUtils.TencentMapUtils;
import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
* @Author Jsxs
* @Date 2023/4/4 20:28
* @PackageName:com.jsxs
* @ClassName: getLineByAdress
* @Description: TODO 地址转经纬度
* @Version 1.0
*/
public class addressResolution {
public static Map<String, Object> getURLContent(String address) {
//这里需要使用你的key值⭐
String urlStr = "https://apis.map.qq.com/ws/geocoder/v1/?address=" + address + "&key=xxxx";
// 1.这里是JSON字符串-通过哇能工具类获取
String result = TencentMapUtils.getTencentMap(urlStr).toString();
// 2.JSON字符串转JSON对象
JSONObject jsonObject = JSONObject.parseObject(result);
// 3. 强制转换 Object转换为 JSONObject-获取键值是: result的值
JSONObject result1 = (JSONObject)jsonObject.get("result");
// 4.在result的基础上继续获取键值是: location的值
JSONObject location = (JSONObject)result1.get("location");
// 5.在location的基础上获取: 经纬度的值
String lng = location.get("lng")+"";
String lat = location.get("lat")+"";
Map<String, Object> map = new HashMap<>();
map.put("lat", lat);
map.put("lng", lng);
return map;
}
public static void main(String[] args) {
Map<String, Object> map = getURLContent("吉林省长春市净月国家高新技术产业开发区福祉大路1016号长春工业大学人文信息学院");
System.out.println("lat=" + map.get("lat") + ",lng=" + map.get("lng"));
}
}
package com.jsxs.TentcentMap.locationSearch;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsxs.TencentMapUtils.TencentMapUtils;
/**
* @Author Jsxs
* @Date 2023/4/5 13:55
* @PackageName:com.jsxs.TentcentMap.locationSearch
* @ClassName: locationSearch
* @Description: TODO 地点搜索
* @Version 1.0
*/
public class locationSearch {
public static JSONArray getLocationSearch(String latitude, String longitude, String radius, String keyword, String page_size, String page_index, String key) {
// latitude->维度 经度->longitude
String keyURL="https://apis.map.qq.com/ws/place/v1/search?boundary=nearby("+latitude+","+longitude+","+radius+")&keyword="+keyword+"&page_size="+page_size+"&page_index="+page_index+"&key="+key;
// 1.JSON字符串
String s = TencentMapUtils.getTencentMap(keyURL).toString();
// 2.JSON字符串转换为JSON对象
JSONObject jsonObject = JSONObject.parseObject(s);
// 3.获取JSON数组
JSONArray data = (JSONArray)jsonObject.get("data");
return data;
}
public static void main(String[] args) {
// 经度 维度 半径/米 关键字 显示条数 显示页数 密钥
JSONArray array = getLocationSearch("40.040589", "116.273543", "1000", "公园", "10", "1", "你的密钥");
// 遍历Json数组
for (int i = 0; i < array.size(); i++) {
System.out.println(array.getJSONObject(i).get("address"));
}
}
}
package com.jsxs.TentcentMap.keywordInputPrompt;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsxs.TencentMapUtils.TencentMapUtils;
/**
* @Author Jsxs
* @Date 2023/4/5 14:45
* @PackageName:com.jsxs.TentcentMap.keywordInputPrompt
* @ClassName: keywordInputPrompt
* @Description: TODO 关键词输入提示
* @Version 1.0
*/
public class keywordInputPrompt {
public static JSONArray getKeywordInputPrompt(String region,String keyword,String key){
String keyURL="https://apis.map.qq.com/ws/place/v1/suggestion/?region="+region+"&keyword="+keyword+"&key="+key;
// 1.JSON字符串
String s = TencentMapUtils.getTencentMap(keyURL).toString();
// 2.JSON字符串转JSON对象
JSONObject jsonObject = JSONObject.parseObject(s);
// 3.获取JSON数组
JSONArray data = (JSONArray)jsonObject.get("data");
return data;
}
public static void main(String[] args) {
// 范围、关键字、密钥
JSONArray array = getKeywordInputPrompt("永川", "超市", "xxx");
for (int i = 0; i < array.size(); i++) {
System.out.println(array.getJSONObject(i).get("title")+" "+array.getJSONObject(i).get("address"));
}
}
}
package com.jsxs.TentcentMap.addressStandardization;
import com.alibaba.fastjson.JSONObject;
import com.jsxs.TencentMapUtils.TencentMapUtils;
/**
* @Author Jsxs
* @Date 2023/4/5 15:20
* @PackageName:com.jsxs.TentcentMap.addressStandardization
* @ClassName: addressStandardization
* @Description: TODO 地址标准化
* @Version 1.0
*/
public class addressStandardization {
public static String getAddressStandardization(String address,String key){
String keyURL="https://apis.map.qq.com/ws/smart_address/address_complete?address="+address+"&key="+key;
// 1.JSON字符串
String s = TencentMapUtils.getTencentMap(keyURL).toString();
// 2.JSON字符串转换为JSON对象
JSONObject jsonObject = JSONObject.parseObject(s);
JSONObject result = (JSONObject)jsonObject.get("result");
String completed_address = result.get("completed_address")+"";
return completed_address;
}
public static void main(String[] args) {
System.out.println(getAddressStandardization("河南周口商水","你的密钥"));
}
}
搞网络通信应用开发的程序员,可能会经常听到外网IP(即互联网IP地址)和内网IP(即局域网IP地址)
我们都知道,IPv4中的IP地址的数量是有限的(所以现在都在搞IPv6嘛),每次把一部分地址分配出去,那么就意味着能够用来分配的IP地址就更少了,而且随着现在手机,电脑等的快速发展,如果每个手机或者电脑都要求一个IP地址,那么显然IP地址是不够用的。
为了解决这个问题,我们可以采取这样的策略:例如对于一个公司来说,每个公司都会有一个属于自己公司的内网(也可以称之为局域网)。
内网(学名应叫局域网(Local Area Network,LAN))是在一个局部的地理范围内,一般可以是是几米内(比如家庭内网),也可以是方圆几千米以内(比如一个大学内网),将各种计算机、外部设备和数据库等互相联接起来组成的计算机通信网。
内网主要作用有:
共享传输信道:简单地理解就是不需要每台电脑一个外网IP地址;
传输速率高:内网之间的电脑因为没有外网网络拓扑的复杂性,所以互相通信的网络可以很快,比如从一个台电脑向另一台电脑复制一个几G的文件可能只需要数十秒时间。
误码率低:因为通信距离很近,所以误码率很低,换句话说就是网络很稳定(老一点的程序员都知道,读大学的时候同一个宿舍内网联网玩C/S游戏,几乎不会遇到断网或卡顿的事情,除非有人下毛片或者把网络给拔了,哈哈)。
package com.jsxs.TentcentMap.ipPositioning;
import com.alibaba.fastjson.JSONObject;
import com.jsxs.TencentMapUtils.TencentMapUtils;
/**
* @Author Jsxs
* @Date 2023/4/5 15:49
* @PackageName:com.jsxs.TentcentMap.ipPositioning
* @ClassName: ipPositioning
* @Description: TODO IP定位
* @Version 1.0
*/
public class ipPositioning {
public static JSONObject getIpPositioning(String ip,String key){
String keyURL="https://apis.map.qq.com/ws/location/v1/ip?ip="+ip+"&key="+key;
String s = TencentMapUtils.getTencentMap(keyURL).toString();
JSONObject jsonObject = JSONObject.parseObject(s);
JSONObject result = (JSONObject)jsonObject.get("result");
JSONObject ad_info = (JSONObject)result.get("ad_info");
return ad_info;
}
public static void main(String[] args) {
// 外网: 我们的服务器的IP就是外网。这里的IP是外网、不是局域网。
System.out.println(getIpPositioning("你的外网IP","你的密钥"));
}
}
注意: (一定要是商业用户,才能调用此接口!!! 本博主没有.)
智能硬件专用工具类
package com.jsxs.TencentMapUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @Author Jsxs
* @Date 2023/4/5 16:33
* @PackageName:com.jsxs.TencentMapUtils
* @ClassName: TencentMapUtilsPost
* @Description: TODO 智能硬件定位专用
* @Version 1.0
*/
public class TencentMapUtilsPost {
//post请求的方法重载
public static StringBuffer TencentUtilsPost(String url,String key,String device_id) throws IOException {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// 设置请求方式为POST
con.setRequestMethod("POST");
// 设置请求参数
String urlParameters = "key="+key+"&device_id="+device_id;
// 发送POST请求
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
// 获取响应结果
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response;
}
}
获取地址!!!!
package com.jsxs.TentcentMap.intelligentHardwarePositioning;
import com.jsxs.TencentMapUtils.TencentMapUtilsPost;
import java.io.*;
public class intelligentHardwarePositioning {
public static void main(String[] args) throws IOException {
String url = "https://apis.map.qq.com/ws/location/v1/network";
String key = "2YPBZ-OLPL3-KW33Z-3KUGO-IOS7S-LSFOX";
String device_id = "D3052B6F-D6AA-EB11-810A-489EBD0BFF28";
StringBuffer response = TencentMapUtilsPost.TencentUtilsPost(url, key, device_id);
// 输出响应结果
System.out.println(response.toString());
}
}
package com.jsxs.TentcentMap.routePlanning;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsxs.TencentMapUtils.TencentMapUtils;
/**
* @Author Jsxs
* @Date 2023/4/5 20:00
* @PackageName:com.jsxs.TentcentMap.routePlanning
* @ClassName: Driving
* @Description: TODO 驾车
* @Version 1.0
*/
public class Driving {
public static JSONArray getDriving(String startLan,String startLon,String endLan,String endLon,String key){
String keyURL="https://apis.map.qq.com/ws/direction/v1/driving/?from="+startLan+","+startLon+"&to="+endLan+","+endLon+"&key="+key;
System.out.println(keyURL);
String s = TencentMapUtils.getTencentMap(keyURL).toString();
// 2. JSON字符串转换为JSON对象
JSONObject jsonObject = JSONObject.parseObject(s);
JSONObject result = (JSONObject)jsonObject.get("result");
JSONArray routes = (JSONArray)result.get("routes");
return routes;
}
public static void main(String[] args) {
JSONArray array = getDriving("39.915285", "116.403857", "39.915285", "116.803857", "你的密钥");
System.out.println("总距离是:"+array.getJSONObject(0).get("distance")+"米");
System.out.println("预计消耗时间是:"+array.getJSONObject(0).get("duration")+"分钟");
// 详细方案的数组
JSONArray steps = (JSONArray)array.getJSONObject(0).get("steps");
for (int i = 0; i < steps.size(); i++) {
System.out.println(steps.getJSONObject(i).get("instruction")+" -》 注意是:"+steps.getJSONObject(i).get("act_desc"));
}
}
}
package com.jsxs.TentcentMap.routePlanning;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsxs.TencentMapUtils.TencentMapUtils;
/**
* @Author Jsxs
* @Date 2023/4/5 20:44
* @PackageName:com.jsxs.TentcentMap.routePlanning
* @ClassName: Walking
* @Description: TODO 步行
* @Version 1.0
*/
public class Walking {
public static JSONArray getWalking(String startLan,String startLon,String endLan,String endLon,String key){
String keyURL="https://apis.map.qq.com/ws/direction/v1/walking/?from="+startLan+","+startLon+"&to="+endLan+","+endLon+"&key="+key;
System.out.println(keyURL);
String s = TencentMapUtils.getTencentMap(keyURL).toString();
// 2. JSON字符串转换为JSON对象
JSONObject jsonObject = JSONObject.parseObject(s);
JSONObject result = (JSONObject)jsonObject.get("result");
JSONArray routes = (JSONArray)result.get("routes");
return routes;
}
public static void main(String[] args) {
JSONArray array = getWalking("39.915285", "116.403857", "39.915285", "116.803857", "你的密钥");
System.out.println("总距离是:"+array.getJSONObject(0).get("distance")+"米");
System.out.println("预计消耗时间是:"+array.getJSONObject(0).get("duration")+"分钟");
// 详细方案的数组
JSONArray steps = (JSONArray)array.getJSONObject(0).get("steps");
for (int i = 0; i < steps.size(); i++) {
System.out.println(steps.getJSONObject(i).get("instruction")+" -》 注意是:"+steps.getJSONObject(i).get("act_desc"));
}
}
}
package com.jsxs.TentcentMap.routePlanning;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsxs.TencentMapUtils.TencentMapUtils;
/**
* @Author Jsxs
* @Date 2023/4/5 20:57
* @PackageName:com.jsxs.TentcentMap.routePlanning
* @ClassName: bicycling
* @Description: TODO
* @Version 1.0
*/
public class bicycling {
public static JSONArray getBicycling(String startLan,String startLon,String endLan,String endLon,String key){
String keyURL="https://apis.map.qq.com/ws/direction/v1/bicycling/?from="+startLan+","+startLon+"&to="+endLan+","+endLon+"&key="+key;
System.out.println(keyURL);
String s = TencentMapUtils.getTencentMap(keyURL).toString();
// 2. JSON字符串转换为JSON对象
JSONObject jsonObject = JSONObject.parseObject(s);
JSONObject result = (JSONObject)jsonObject.get("result");
JSONArray routes = (JSONArray)result.get("routes");
return routes;
}
public static void main(String[] args) {
JSONArray array = getBicycling("39.915285", "116.403857", "39.915285", "116.803857", "你的密钥");
System.out.println("总距离是:"+array.getJSONObject(0).get("distance")+"米");
System.out.println("预计消耗时间是:"+array.getJSONObject(0).get("duration")+"分钟");
// 详细方案的数组
JSONArray steps = (JSONArray)array.getJSONObject(0).get("steps");
for (int i = 0; i < steps.size(); i++) {
System.out.println(steps.getJSONObject(i).get("instruction")+" -》 注意是:"+steps.getJSONObject(i).get("act_desc"));
}
}
}
差异化 很严重...
package com.jsxs.TentcentMap.routePlanning;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsxs.TencentMapUtils.TencentMapUtils;
/**
* @Author Jsxs
* @Date 2023/4/5 21:02
* @PackageName:com.jsxs.TentcentMap.routePlanning
* @ClassName: transit
* @Description: TODO
* @Version 1.0
*/
public class transit {
public static JSONArray getTransit(String startLan,String startLon,String endLan,String endLon,String key){
String keyURL="https://apis.map.qq.com/ws/direction/v1/transit/?from="+startLan+","+startLon+"&to="+endLan+","+endLon+"&key="+key;
System.out.println(keyURL);
String s = TencentMapUtils.getTencentMap(keyURL).toString();
// 2. JSON字符串转换为JSON对象
JSONObject jsonObject = JSONObject.parseObject(s);
JSONObject result = (JSONObject)jsonObject.get("result");
JSONArray routes = (JSONArray)result.get("routes");
return routes;
}
public static void main(String[] args) {
JSONArray array = getTransit("39.915285", "116.403857", "39.915285", "116.803857", "xxxxxxx");
System.out.println("总距离是:"+array.getJSONObject(0).get("distance")+"米");
System.out.println("预计消耗时间是:"+array.getJSONObject(0).get("duration")+"分钟");
// 详细方案的数组
JSONArray steps = (JSONArray)array.getJSONObject(0).get("steps");
// 1.步行
JSONArray steps1 = (JSONArray)steps.getJSONObject(0).get("steps");
System.out.println("*************导航开始*************");
for (int i = 0; i < steps1.size(); i++) {
System.out.println(steps1.getJSONObject(i).get("instruction")+" -》 注意是:"+steps1.getJSONObject(i).get("act_desc"));
if (i==steps1.size()-1){
System.out.println(steps1.getJSONObject(i).get("instruction"));
System.out.println("*************步行已截至*************");
}
}
// 2.地铁
JSONArray lines = (JSONArray)steps.getJSONObject(1).get("lines");
for (int i = 0; i < lines.size(); i++) {
JSONArray stations = (JSONArray)lines.getJSONObject(i).get("stations");
System.out.println("乘坐"+lines.getJSONObject(i).get("title")+" 经过站点个数为:"+lines.getJSONObject(i).get("station_count")+" 预计消费"+lines.getJSONObject(i).get("price")+"分人民币");
System.out.println("站点分别是: ");
for (int i1 = 0; i1 < stations.size(); i1++) {
System.out.print(stations.getJSONObject(i1).get("title")+"->");
if (i1==stations.size()-1){
System.out.println(stations.getJSONObject(i1).get("title"));
System.out.println("*************地铁已截至*************");
}
}
}
// 3. ....后续...暂且不更新
}
}
每日只能调用一次数据
package com.jsxs.TentcentMap.routePlanning;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsxs.TencentMapUtils.TencentMapUtils;
/**
* @Author Jsxs
* @Date 2023/4/6 20:17
* @PackageName:com.jsxs.TentcentMap.routePlanning
* @ClassName: Truck
* @Description: TODO 货车: 一日只能调用一次...
* @Version 1.0
*/
public class Truck {
public static JSONArray getTruck(String startLn,String startLo,String endLn,String endLo,String key){
String keyURL="https://apis.map.qq.com/ws/direction/v1/trucking?from="+startLn+","+startLo+"&to="+endLn+","+endLo+"&key="+key;
System.out.println(keyURL);
String s = TencentMapUtils.getTencentMap(keyURL).toString();
JSONObject jsonObject = JSONObject.parseObject(s);
JSONObject result = (JSONObject)jsonObject.get("result");
JSONArray routes = (JSONArray)result.get("routes");
return routes;
}
public static void main(String[] args) {
JSONArray array = getTruck("40.023408", "116.318436", "39.767381", "116.337662", "xxxxxx");
System.out.println("总距离:"+array.getJSONObject(0).get("distance")+"米 预计消耗时间"+array.getJSONObject(0).get("duration")+"分钟");
for (int i = 0; i < array.size(); i++) {
JSONArray steps = (JSONArray)array.getJSONObject(i).get("steps");
for (int i1 = 0; i1 < steps.size(); i1++) {
System.out.println(i1+" "+steps.getJSONObject(i1).get("instruction"));
}
}
}
}
package com.jsxs.TentcentMap.AdministrativeDivision;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsxs.TencentMapUtils.TencentMapUtils;
/**
* @Author Jsxs
* @Date 2023/4/6 20:48
* @PackageName:com.jsxs.TentcentMap.AdministrativeDivision
* @ClassName: provincialAndUrbanAreas
* @Description: TODO 全国省市区
* @Version 1.0
*/
public class provincialAndUrbanAreas {
public static JSONArray getProvincialAndUrbanAreas(String key) {
String urlKey="https://apis.map.qq.com/ws/district/v1/list?key="+key;
System.out.println(urlKey);
String s = TencentMapUtils.getTencentMap(urlKey).toString();
JSONObject jsonObject = JSONObject.parseObject(s);
JSONArray result = (JSONArray)jsonObject.get("result");
// System.out.println(result);
return result;
}
public static void main(String[] args) {
JSONArray array = getProvincialAndUrbanAreas("xxx");
// 数组里面存放着数组。然后第二个数组存放着对象
JSONArray jsonArray = array.getJSONArray(0);
for (int i = 0; i < jsonArray.size(); i++) {
System.out.println("邮政编号:"+jsonArray.getJSONObject(i).get("id")+"->"+jsonArray.getJSONObject(i).get("fullname"));
}
}
}
package com.jsxs.TentcentMap.AdministrativeDivision;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsxs.TencentMapUtils.TencentMapUtils;
/**
* @Author Jsxs
* @Date 2023/4/6 21:12
* @PackageName:com.jsxs.TentcentMap.AdministrativeDivision
* @ClassName: subAdministrativeDivision
* @Description: TODO 子级行政区划
* @Version 1.0
*/
public class subAdministrativeDivision {
public static JSONArray getSubAdministrativeDivision(String id,String key){
String keyURL="https://apis.map.qq.com/ws/district/v1/getchildren?id="+id+"&key="+key;
System.out.println(keyURL);
String s = TencentMapUtils.getTencentMap(keyURL).toString();
JSONObject jsonObject = JSONObject.parseObject(s);
JSONArray result = (JSONArray)jsonObject.get("result");
return result;
}
public static void main(String[] args) {
JSONArray array = getSubAdministrativeDivision("410000", "xxxx");
JSONArray jsonArray = array.getJSONArray(0);
for (int i = 0; i < jsonArray.size(); i++) {
System.out.println("邮政编号:"+jsonArray.getJSONObject(i).get("id")+"->"+jsonArray.getJSONObject(i).get("fullname"));
}
}
}
package com.jsxs.TentcentMap.AdministrativeDivision;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jsxs.TencentMapUtils.TencentMapUtils;
/**
* @Author Jsxs
* @Date 2023/4/6 21:22
* @PackageName:com.jsxs.TentcentMap.AdministrativeDivision
* @ClassName: keywordsAdministrativeDivision
* @Description: TODO 关键词行政区划
* @Version 1.0
*/
public class keywordsAdministrativeDivision {
public static JSONArray getKeywordsAdministrativeDivision(String keywords,String key){
String keyURL="https://apis.map.qq.com/ws/district/v1/search?&keyword="+keywords+"&key="+key;
String s = TencentMapUtils.getTencentMap(keyURL).toString();
JSONObject jsonObject = JSONObject.parseObject(s);
JSONArray result = (JSONArray)jsonObject.get("result");
return result;
}
public static void main(String[] args) {
JSONArray array = getKeywordsAdministrativeDivision("周口", "xxxx");
JSONArray jsonArray = array.getJSONArray(0);
for (int i = 0; i < jsonArray.size(); i++) {
System.out.println("邮政编号:"+jsonArray.getJSONObject(i).get("id")+"->"+jsonArray.getJSONObject(i).get("fullname")+" ->详细地址:"+jsonArray.getJSONObject(i).get("address"));
}
}
}
概述
阿里巴巴提供的一个json快速转换工具类,实现了Map接口,通过Key Value形式存储数据,初始化大小为16,有参构造方法需要传入一个Map。
项目中实际用途:
用于Json转具体的Object实体类,或者String快速转JSON格式,都可以用或者有的数据属性变化较多,暂时不想定义一个POJO接参,就可以用JSONObject先接着。
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>2.0.26version>
dependency>
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
import java.util.Map;
/**
* @Author Jsxs
* @Date 2023/4/5 5:59
* @PackageName:PACKAGE_NAME
* @ClassName: testJSON
* @Description: TODO
* @Version 1.0
*/
public class testJSON {
public static void main(String[] args) {
Student student = new Student("李明",21);
// 1. Java对象转化为JSON字符串
String s = JSONObject.toJSONString(student); // 第一个参数是: Java对象
System.out.println("1.对象转换为JSON字符串-> "+s);
// 2. Java对象转换为Object对象
Object o = JSONObject.toJSON(student); // 第一个参数是: Java对象
System.out.println("2.Java对象转换为Object对象-> "+o);
// 3. (JSON)字符串转换为JSON对象 ⭐
JSONObject jsonObject = JSONObject.parseObject(s); // 第一个参数是: JSON字符串
System.out.println("3.JSON字符串转换为JSON对象-> "+jsonObject);
// 4. JSON字符串转换为Object对象
Object parse = JSONObject.parse(s); // 第一个参数是: JSON字符串
System.out.println("4.JSON字符串转换为Object对象-> "+parse);
// 5.JSON对象转换为Java对象
Student student1 = JSONObject.toJavaObject(jsonObject, Student.class);// 第一个参数是: json对象;第二个参数是: 类名.class
System.out.println("5.JSON对象转换为Java对象-> "+student1);
// 6.JSON字符串转换为Map类型
Map map = JSONObject.parseObject(s, Map.class); // 第一个参数是: JSON字符串;第二个参数是: Map.class
System.out.println("6.JSON字符串转换为List类型-> "+map);
// 7.JSON对象转换为List类型的JSON集合
List lists = JSONObject.parseArray(JSONObject.toJSONString(jsonObject), List.class); // 第一个参数是JSON字符串;第二个是List.class
System.out.println("7.JSON对象转换为List类型的JSON集合-> "+lists);
// 8.JSON类型的集合转
// // 9.JSON字符串转JSON数组 ----- 没有运行成功!!!!
// JSONArray objects = JSONObject.parseArray(s); // 第一个参数是: JSON数组
// System.out.println("9.JSON字符串转JSON数组-> "+objects);
// 10.JSON字符串转Java对象
Student student2 = JSONObject.parseObject(s, Student.class);// 第一个参数是: JSON字符串;第二个参数是: 类.class
System.out.println("10.JSON字符串转Java对象-> "+student2);
}
}
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author Jsxs
* @Date 2023/4/5 5:59
* @PackageName:PACKAGE_NAME
* @ClassName: testJSON
* @Description: TODO
* @Version 1.0
*/
public class testJSON {
public static void main(String[] args) throws Exception {
// (1)通过无参构造直接设值
JSONObject object = new JSONObject();
object.put("id",123);
object.put("name","jojo");
System.out.println("JSONObject:"+object.toString());
// (2)将JSON对象转成具体的POJO
Student student = JSONObject.toJavaObject(object, Student.class);
System.out.println("转换成Demo实体类:"+student.toString());
// (3)传入Map的有参构造方法创建
Map<String,Object> myMap = new HashMap<>(2);
myMap.put("id",123);
myMap.put("name","test");
JSONObject newObject = new JSONObject(myMap);
System.out.println("通过有参构造传入Map创建出来的:"+newObject);
// (4)将String转JSONObject
String demoString = "{name:\"test\", id:123}";
JSONObject demoJson = JSONObject.parseObject(demoString);
System.out.println("String转Object : "+demoJson);
}
}
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author Jsxs
* @Date 2023/4/5 5:59
* @PackageName:PACKAGE_NAME
* @ClassName: testJSON
* @Description: TODO
* @Version 1.0
*/
public class testJSON {
public static void main(String[] args) throws Exception {
String listStr = "[{\"id\":1,\"name\":\"vivi\"},{\"id\":2,\"name\":\"jojo\"}]";
// (1)转Json List
JSONArray jsonArray = JSONArray.parseArray(listStr);
System.out.println(jsonArray);
// (2)转具体的List<实体类>
List<Student> listDemo = jsonArray.toJavaList(Student.class);
for (Student demo : listDemo){
System.out.println(demo.toString());
}
}
}
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author Jsxs
* @Date 2023/4/5 5:59
* @PackageName:PACKAGE_NAME
* @ClassName: testJSON
* @Description: TODO
* @Version 1.0
*/
public class testJSON {
public static void main(String[] args) throws Exception {
String listStr = "[{\"id\":1,\"name\":\"vivi\"},{\"id\":2,\"name\":\"jojo\"}]";
// (1)转Json List
JSONArray jsonArray = JSONArray.parseArray(listStr);
System.out.println(jsonArray);
// (2) 循环遍历JsonArray
for (int i = 0; i < jsonArray.size(); i++) {
System.out.println(jsonArray.getJSONObject(i));
System.out.println(jsonArray.getJSONObject(i).get("name"));
}
}
}
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author Jsxs
* @Date 2023/4/5 5:59
* @PackageName:PACKAGE_NAME
* @ClassName: testJSON
* @Description: TODO
* @Version 1.0
*/
public class testJSON {
public static void main(String[] args) throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put("1","a");
jsonObject.put("2","b");
jsonObject.put("3","c");
for (String a : jsonObject.keySet()){
System.out.println("key:"+a+";Value:"+jsonObject.get(a));
}
}
}