前端之家收集整理的这篇文章主要介绍了
使用org.json解析较复杂的百度API附近的银行等地方,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.org.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class ServerUtils {
private static String content = "",line=System.getProperty("line.separator");//换行相当于\n
public static String getContent(String url){
HttpClient client=new DefaultHttpClient();
HttpEntity httpEntity=null;
String result="";
try {
HttpGet post=new HttpGet(url);
HttpResponse httpResponse = client.execute(post);
httpEntity=httpResponse.getEntity();
if(httpEntity!=null){
result=EntityUtils.toString(httpEntity,"UTF-8").trim();
return result;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
httpEntity.consumeContent();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 读文件流
* @param formPath从哪里读取的文件路径
* @return
*/
public static String readerFile(String formPath) {
FileReader read = null;
BufferedReader reader = null;
try {
read = new FileReader(new File(formPath));
reader = new BufferedReader(read);
StringBuffer buffer = new StringBuffer("");
content = reader.readLine();
while (content != null) {
buffer.append(content).append(line);
content = reader.readLine();
}
return content = buffer.toString();//返回
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (reader != null)reader.close();
if (read != null)read.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return "";//没值就返回空
}
public static void main(String[] args) {
String Url="http://api.k780.com:88/?app=entry.qihu&domain=www.baidu.com&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";
System.out.println(getContent(Url));
}
}
package com.org.json;
import java.net.URLEncoder;
import org.json.JSONArray;
import org.json.JSONObject;
import com.org.utils.ServerUtils;
/**
* @Author:liangjilong
* @Date:2014-4-28
* @Version:1.0
* @Descript:从网络上抓去下来解析json数据
*/
public class TestJson1 {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
String key="D4tWvZgUrICf3oga0Q0udddk";
String url= getUrl("银行",key); //即搜索经纬度39.915,116.404,39.975,116.414的附近的银行
String json = ServerUtils.getContent(url);
JSONObject jsonObj = new JSONObject(json);
String status = jsonObj.get("status").toString();// 解析status节点
// System.out.println(status);
String results=jsonObj.get("results").toString();//results节点
JSONArray resultArrs=new JSONArray(results);
for (int i = 0; i < resultArrs.length(); i++) {
JSONObject resultObj=(JSONObject)resultArrs.get(i);
String name="第"+i+resultObj.get("name");//results下的name节点
String address=resultObj.getString("address");////results下的address节点
if(resultObj.has("telephone")){//判断是否有这个节点
String telephone=resultObj.getString("telephone");////results下的telephone节点
System.out.println(telephone);
}
String detail_url=resultObj.get("detail_url").toString();
if(resultObj.has("tag")){//判断是否有这个节点有就取出来
String tag=resultObj.get("tag").toString();
}
JSONObject locationObj=(JSONObject)resultObj.get("location");//location节点
String lat=locationObj.getString("lat");//location节点lat经度
String lng=locationObj.getString("lng");//location节点lng伟度
System.out.println(lng);
}
}
/**
*
* @param keyWord搜索的地方
* @param key百度申请的ak密钥
* @return
* @throws Exception
*/
public static String getUrl(String keyWord,String key)throws Exception{
StringBuffer buffer=new StringBuffer();
buffer.append("http://api.map.baidu.com/place/search?");
buffer.append("&query="+URLEncoder.encode(keyWord,"utf-8"));
buffer.append("&bounds="+"39.915,116.414");//经纬度
buffer.append("&output=json");//输出格式(JSON/XML)
buffer.append("&key="+key);
return buffer.toString();
}
}
package com.org.json;
import org.json.JSONArray;
import org.json.JSONObject;
import com.org.utils.ServerUtils;
/**
* @Author:liangjilong
* @Date:2014-4-28
* @Version:1.0
* @Descript:从本地的文本读取文件下来解析json数据
*/
public class TestJson2 {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
String path=TestJson2.class.getClassLoader().getResource("json.txt").getFile();
String json = ServerUtils.readerFile(path);
JSONObject jsonObj = new JSONObject(json);
String status = jsonObj.get("status").toString();// 解析status节点
// System.out.println(status);
String results=jsonObj.get("results").toString();//results节点
JSONArray resultArrs=new JSONArray(results);
for (int i = 0; i < resultArrs.length(); i++) {
JSONObject resultObj=(JSONObject)resultArrs.get(i);
String name="第"+i+resultObj.get("name");//results下的name节点
String address=resultObj.getString("address");////results下的address节点
if(resultObj.has("telephone")){//判断是否有这个节点
String telephone=resultObj.getString("telephone");////results下的telephone节点
System.out.println(telephone);
}
String detail_url=resultObj.get("detail_url").toString();
if(resultObj.has("tag")){//判断是否有这个节点
String tag=resultObj.get("tag").toString();
}
JSONObject locationObj=(JSONObject)resultObj.get("location");//location节点
String lat=locationObj.getString("lat");//location节点lat经度
String lng=locationObj.getString("lng");//location节点lng伟度
System.out.println(lng);
}
}
}