java读写json测试实现手机地归属地的查询
今天突然想玩玩json,苦于没有素材,忽然想到一般查询手机归属地内容应该很好用json格式保存,在网上找到了淘宝的归属地API,并下了处理json相关的jar包,动手玩了起来。整个例子挺简单的,只是在java处理字符乱码时稍微理了下头绪。
View java Code
1 package com.think.java;2
3 import java.io.BufferedReader;
4 import java.io.InputStream;
5 import java.io.InputStreamReader;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.util.ArrayList;
9 import java.util.List;
10 import net.sf.json.JSONArray;
11 import net.sf.json.JSONObject;
12
13 public class TestMobileCity {
14
15 /**
16 * 测试手机号码是来自哪个城市的,利用淘宝的API
17 * @param mobileNumber 手机号码
18 * @return
19 * @throws MalformedURLException
20 */
21 public static String calcMobileCity(String mobileNumber) throws MalformedURLException{
22 String jsonString = null;
23 JSONArray array = null;
24 JSONObject jsonObject = null;
25 String urlString = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + mobileNumber;
26 StringBuffer sb = new StringBuffer();
27 BufferedReader buffer;
28 URL url = new URL(urlString);
29 try{
30 InputStream in = url.openStream();
31
32 // 解决乱码问题
33 buffer = new BufferedReader(new InputStreamReader(in,"gb2312"));
34 String line = null;
35 while((line = buffer.readLine()) != null){
36 sb.append(line);
37 }
38 in.close();
39 buffer.close();
40 // System.out.println(sb.toString());
41 jsonString = sb.toString();
42 // 替换掉“__GetZoneResult_ = ”,让它能转换为JSONArray对象
43 jsonString = jsonString.replaceAll("^[__]\\w{14}+[_ = ]+", "[");
44 // System.out.println(jsonString+"]");
45 String jsonString2 = jsonString + "]";
46 // 把STRING转化为json对象
47 array = JSONArray.fromObject(jsonString2);
48
49 // 获取JSONArray的JSONObject对象,便于读取array里的键值对
50 jsonObject = array.getJSONObject(0);
51
52 }catch(Exception e){
53 e.printStackTrace();
54 }
55 return jsonObject.getString("province");
56 }
57
58 /**
59 * 计算多个号码的归属地
60 * @param mobileNumbers 号码列表
61 * @return
62 * @throws MalformedURLException
63 */
64 public static JSONObject calcMobilesCities(List<String> mobileNumbers) throws MalformedURLException{
65 JSONObject jsonNumberCity = new JSONObject();
66 for(String mobileNumber : mobileNumbers){
67 jsonNumberCity.put(mobileNumber, calcMobileCity(mobileNumber)); ;
68 }
69 return jsonNumberCity;
70 }
71
72 public static void main(String[] args) throws Exception{
73 String testMobileNumber = "1881758452";
74 System.out.println(calcMobileCity(testMobileNumber));
75 List<String> mobileList = new ArrayList<String>();
76 for(int i = 1350345; i < 1350388; i++){
77 mobileList.add(String.valueOf(i));
78 }
79 System.out.println(calcMobilesCities(mobileList).toString());
80 }
81 }
附:
json相关jar包
原文出处:http://www.cnblogs.com/caoyuanzhanlang
==========================================================================================================
=================================== 以上代码纯属个人的娱乐,欢迎指正与交流 ===================================
=================================== 尊重劳动成果,转载请注明出处,万分感谢 ===================================
==========================================================================================================
以上是 java读写json测试实现手机地归属地的查询 的全部内容, 来源链接: utcz.com/z/394657.html