如何用java获取百度API返回的java请求返回json数据据的?

2550人阅读
百度API市场提供了大量免费同时又非常好用的第三方API。关于其API的调用等问题都有相应的示例非常的方便。
关于其返回的JSON数据的解析,笔者遇到了这样的一个问题。
例:天气预报API返回数据形式为:
JSON返回示例 :
errNum: 0,
errMsg: "success",
retData: {
city: "北京",
pinyin: "beijing",
citycode: "",
date: "15-02-11",
time: "11:00",
postCode: "100000",
longitude: 116.391,
latitude: 39.904,
altitude: "33",
weather: "晴",
temp: "10",
l_tmp: "-4",
h_tmp: "10",
WD: "无持续风向",
WS: "微风(&10m/h)",
sunrise: "07:12",
sunset: "17:44"
请将apikey作为参数添加到header中;
当返回{"errNum":300003,"errMsg":"url is not parse"} 时,请校验是否传入apikey;
为了取到其中的各条信息需要用到JSON数据的解析。
例:获取城市名称
$cityname = urlencode($_POST['cityname']);
$url = '/apistore/weatherservice/cityname?cityname='.$cityname;
$ch = curl_init();
$header = array(
'apikey: e858d877f6febd23ddbf220',
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER
, $header);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$output = curl_exec($ch);
curl_close($ch);
$res = json_decode($output,true);
$this-&assign('city',$res["retData"]['city']);
$this-&assign('date',$res["retData"]['date']);
$this-&assign('time',$res["retData"]['time']);
$this-&assign('temp',$res["retData"]['temp']);
$this-&assign('l_tmp',$res["retData"]['l_tmp']);
$this-&assign('h_tmp',$res["retData"]['h_tmp']);
$this-&assign('weather',$res["retData"]['weather']);
$this-&assign('WS',$res["retData"]['WS']);
$this-&display();
上面为一组关于利用天气预报API获取相关信息的PHP代码。其中利用到ThinkPHP模板技术。
笔者想说的是其中解析JSON数据部分,利用的是
$city = $res["resData"]["city"];
通过这种方式就可以获取其中的每条信息。这种方式基本可以处理所有API返回数据。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:107555次
积分:1671
积分:1671
排名:千里之外
原创:60篇
转载:19篇
(1)(2)(3)(2)(16)(2)(1)(7)(20)(6)(15)(4)BaiduAPI java调用百度地图API反解析经纬码,通过 json文件返回地址信息 Develop 238万源代码下载-
&文件名称: BaiduAPI
& & & & &&]
&&所属分类:
&&开发工具: Java
&&文件大小: 238 KB
&&上传时间:
&&下载次数: 4
&&提 供 者:
&详细说明:java调用百度地图API反解析经纬码,通过解析json文件返回地址信息-java call Baidu Maps API anti-analytic Jingwei code, by parsing json file the return address information.
文件列表(点击判断是否您需要的文件,如果是垃圾请在下面评价投诉):
&&BaiduAPI\BaiduAPI.java&&........\commons-lang-2.4.jar&&BaiduAPI
&近期下载过的用户:
&输入关键字,在本站238万海量源码库中尽情搜索:
&[] - 利用MATLAB实现空间直角坐标系与大地坐标系转换
&[] - 百度手机地图API类参考,适用于对百度地图开发API中类方法使用不理解的人阅读。百度手机号码归属地查询api与返回json处理_Java教程_动态网站制作指南
百度手机号码归属地查询api与返回json处理
来源:人气:4576
百度手机号码归属地查询api与返回json处理前天无意间在网上看到百度ApiStore,然后好奇就进去看了看。正好最近在某博培训Android,刚学到基础。抱着锻炼的心态选择手机号码归属地查询api进行练手。api地址 (/apistore/mobilephoneservice/mobilephone)。百度官方已经给出请求示例 。我们只需要对请求结果json进行解析就可以。Java请求示例: 1 String httpUrl = "/apistore/mobilephoneservice/mobilephone"; 2 String httpArg = "tel="; 3 String jsonResult = request(httpUrl, httpArg); 4 System.out.intln(jsonResult); 5
* @param urlAll 8
:请求接口 9
* @param httpArg10
* @return 返回结果12
*/13 public static String request(String httpUrl, String httpArg) {14
BufferedReader reader =15
String result =16
StringBuffer sbf = new StringBuffer();17
httpUrl = httpUrl + "?" + httpA18 19
URL url = new URL(httpUrl);21
HttpURLConnection connection = (HttpURLConnection) url22
.openConnection();23
connection.setRequestMethod("GET");24
// 填入apikey到HTTP header25
connection.setRequestProperty("apikey",
"您自己的apikey");26
connection.connect();27
InputStream is = connection.getInputStream();28
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));29
String strRead =30
while ((strRead = reader.readLine()) != null) {31
sbf.append(strRead);32
sbf.append("\r\n");33
reader.close();35
result = sbf.toString();36
} catch (Exception e) {37
e.printStackTrace();38
}3940 }我们要做的是进行对请求返回结果result处理,百度给出了返回结果为json,那么就要对json进行解析输出。 百度给出的json返回示例:1 {2 errNum: 0,3 errMsg: "success",4 retData: {5
telString: "", //手机号码6
province: "黑龙江",
carrier: "黑龙江移动"
}9 } 对json解析需要用到json-lib.jar包,网上可以百度到。 json解析核心代码:1 JSONObject obj = JSONObject.fromObject(jsonResult1); 2 String errNum = obj.getString("errNum"); 演示示例: 1 package day02; 2
3 import java.io.BufferedR 4 import java.io.InputS 5 import java.io.InputStreamR 6 import java.net.HttpURLC 7 import java.net.URL; 8 import net.sf.json.JSONO 9 10 public class Test11 {11 12
* 查询手机号码归属地14
* @param args15
public static void main(String[] args) {17
// TODO Auto-generated method stub18 19
String httpUrl = "/apistore/mobilephoneservice/mobilephone";20
String httpArg = "tel=";21
String jsonResult1 = request(httpUrl, httpArg);22
System.out.println(jsonResult1);23
JSONObject obj = JSONObject.fromObject(jsonResult1);24
String errNum = obj.getString("errNum");25
System.out.println(errNum);26
String errMsg = obj.getString("errMsg");27
System.out.println(errMsg);28
String retData = obj.getString("retData");29
JSONObject obj2 = JSONObject.fromObject(retData);30
String telString = obj2.getString("telString");31
String province = obj2.getString("province");32
String carrier = obj2.getString("carrier");33
System.out.println("你查询号码:"+telString+"\n"+"归属地:"+province+"\n"+"运营商:"+carrier);34 35
* @param urlAll39
:请求接口40
* @param httpArg41
* @return 返回结果43
public static String request(String httpUrl, String httpArg) {45
BufferedReader reader =46
String result =47
StringBuffer sbf = new StringBuffer();48
httpUrl = httpUrl + "?" + httpA49 50
URL url = new URL(httpUrl);52
HttpURLConnection connection = (HttpURLConnection) url53
.openConnection();54
connection.setRequestMethod("GET");55
// 填入apikey到HTTP header56
connection.setRequestProperty("apikey",57
"你的百度api秘钥");58
connection.connect();59
InputStream is = connection.getInputStream();60
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));61
String strRead =62
while ((strRead = reader.readLine()) != null) {63
sbf.append(strRead);64
sbf.append(strRead + " ");65
reader.close();67
result = sbf.toString();68
} catch (Exception e) {69
e.printStackTrace();70
}73 74 }输出结果:1 {"errNum":0,"errMsg":"success","retData":{"telString":"","province":"\u6cb3\u5357","carrier":"\u6cb3\u5357\u79fb\u52a8"}}{"errNum":0,"errMsg":"success","retData":{"telString":"","province":"\u6cb3\u5357","carrier":"\u6cb3\u5357\u79fb\u52a8"}} 2 错误码:03 错误码返回:success4 你查询号码: 归属地:河南6 运营商:河南移动自此百度手机号码归属地api体验成功结束,也是实训期间的练手。
优质网站模板匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 java实现返回json数据 的文章

 

随机推荐