/** * 获取网络的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包
相关推荐
JSON、GSON解析json数据,详细介绍了json和gson的使用,解析复杂json数据
Json解析,Gson解析,本地,网络解析简单的案例
解放你的双手,不用再去一个一个JSONObject去处理解析问题,两行代码搞定一切
通过Gson解析Json文件,相关博文:http://www.cnblogs.com/tianzhijiexian/p/4246497.html
这里进行介绍Gson解析复杂Json和简单Json的数据方式
能够便利拼接json字符串,方便程序员更加整理大型json。
1、生成Json数据 2、使用Gson解析Json数据(单个json对象、json数组和json对象内嵌json数组) // 这里有两篇好心人的博文,有详细的讲解 http://www.jb51.net/article/32547.htm ...
Gson解析(List和Map)格式json数据 - CSDN博客原创2013年11月23日 21:54:46Gson解析(List和Map)格式json数据天
android之json和gson数据解析最完整的代码例子(包括各种样式的json数据) 本人在2.3.3模拟器上已亲测可以运行。最适合学习json 或gson数据解析的初学者。
Android Studio下使用Google Gson解析Json数据示例
Gson解析复杂json数据,复杂的json数据用gson工具解析
java开发中使用JSON和gson所需要的jar,里面包含了所有的jar,这样我们就可以随便使用JSON和gson了
使用gson解析json的一次测试使用
gson解析json 1gson-2.0.jar 2log4j-1.2.17.jar 3slf4j-api-1.7.10.jar 4slf4j-log4j12-1.7.10.jar
Gson解析JSON数据的入门级源码
java解析json所需的三个jar包。包含org.json、Gson、fastjson三个jar包
Gson解析嵌套json
该源码是用Gson解析一个复杂的json,将json中的数据加载到listview中并且根据json中的url地址加载网络图片到listview中。
Gson解析json,实现泛型解析数据,根据用户传入的类型,解析称用户需要的,Common \CommonList 里面的status message data相关的约定可以更具开发者与服务器的约定修改