使用org.json解析较复杂的百度API附近的银行等地方

来源:互联网 发布:cms建站 手机与pc 编辑:程序博客网 时间:2024/04/30 05:10
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.404,39.975,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);} }}


0 0
原创粉丝点击