`

获取网络的json文件 Gson解析json文件

    博客分类:
  • java
 
阅读更多
/**
	 * 获取网络的json文件
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String getJSONData(String url)
			throws ClientProtocolException, IOException {
		String result = "";
		URL url2 = null;
		try {
			url2 = new URL(url);
		} catch (MalformedURLException e) {
			System.out.println("getJSONData" + "MalformedURLException");
		}
		if (url2 != null) {
			try {
				// 使用HttpURLConnection打开连接
				HttpURLConnection urlConn = (HttpURLConnection) url2
						.openConnection();
				urlConn.setRequestProperty("Accept-Encoding", "gzip,deflate");
				urlConn.connect();
				String str = urlConn.getContentEncoding();
				InputStream is;
				if (str != null) {
					if (str.equalsIgnoreCase("gzip")) {
						is = new GZIPInputStream(urlConn.getInputStream());
					} else {
						is = urlConn.getInputStream();
					}

				} else {
					is = urlConn.getInputStream();
				}
				// 得到读取的内容
				InputStreamReader in = new InputStreamReader(is, "UTF-8");
				// 为输出创建BufferReader
				BufferedReader buffer = new BufferedReader(in);
				String inputLine = null;
				// 使用循环来读取获得的数据
				while ((inputLine = buffer.readLine()) != null) {
					// 我们在每一行后面加\n
					result += inputLine + "\n";
				}
				// 关闭InputStreamReader
				in.close();
				// 关闭http连接
				urlConn.disconnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

 

	/**
	  * 
	  * @param path 文件夹路径
	  */
	 public static void isExist(String path) {
	  File file = new File(path);
	    //判断文件夹是否存在,如果不存在则创建文件夹
	  if (!file.exists()) {
	   file.mkdir(); 
	   System.out.println("----创建文件夹成功.");
	  }else{
		  System.out.println("----文件夹存在.");
	  }
	  
	 }

 

 

json文件格式:

{"page":1,"results":[{"adult":false,"backdrop_path":"/aKdfFihGoCCUokcR5ZeotwzFW5g.jpg","id":202220,"original_title":"不二神探","release_date":"2013-12-31","poster_path":"/bXd9v2fSVZih0frr2kv2Mt1LBHq.jpg","popularity":1.44325521613959,"title":"不二神探","vote_average":2.5,"vote_count":14}],"total_pages":1,"total_results":1}

 

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 Gson gson = new Gson();
		try {
            String jsonUrl = getJSONData(path);
            VideoInfo info = gson.fromJson(jsonUrl, VideoInfo.class);
                    int len = info.getResults().size();           
                    for(int i=0; i<len; i++) {
            
                        System.out.println("Backdrop_path:"+info.getResults().get(i).getBackdrop_path());
                        System.out.println("title:"+info.getResults().get(i).getTitle());
                        System.out.println("Id:"+info.getResults().get(i).getId());
                        System.out.println("date:"+info.getResults().get(i).getRelease_date());
            
                    }

            System.out.println(jsonUrl);
            System.out.println("OK.........");
		} catch (ClientProtocolException e) {
			System.out.println("ClientProtocolException.........");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("IOException.........");
			e.printStackTrace();
		}
	}

 

   附件就是gson解析必须有三的jar包

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics