使用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 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);
			
		}
		 
	}

}

 

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="D4tWvZgUrICf3oga0Q0uT5sk";	
		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();
	}

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章