当前位置:   article > 正文

学习记录:java 请求接口获取返回的json信息中json数据保存至list集合_java 解析json参数放到list里面

java 解析json参数放到list里面

首先将以下需要的包导入


gson-2.8.0.jar
commons-beanutils-1.8.0.jar
commons-collections-3.2.1.jar
commons-lang-2.4.jar
commons-logging.jar
ezmorph-1.0.6.jar

 

附上下载地址链接https://pan.baidu.com/s/1z5gOjYrzNNXJNYdMosI0yA  提取码:6732 
贴上代码

  1. package music;
  2. import com.google.gson.JsonArray;
  3. /**
  4. * Created by david on 2017-7-5.
  5. */
  6. import com.google.gson.JsonObject;
  7. import com.google.gson.JsonParser;
  8. import net.sf.json.JSONArray;
  9. import net.sf.json.JSONObject;
  10. import java.io.BufferedInputStream;
  11. import java.io.BufferedReader;
  12. import java.io.ByteArrayOutputStream;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.io.InputStreamReader;
  16. import java.io.PrintWriter;
  17. import java.net.HttpURLConnection;
  18. import java.net.URL;
  19. import java.util.List;
  20. import net.sf.json.JsonConfig;
  21. public class paihang {
  22. /**
  23. * 发起http请求并获取结果
  24. * @param requestUrl 请求地址
  25. */
  26. public static JSONObject getXpath(String requestUrl){
  27. String res="";
  28. JSONObject object = null;
  29. StringBuffer buffer = new StringBuffer();
  30. try{
  31. URL url = new URL(requestUrl);
  32. HttpURLConnection urlCon= (HttpURLConnection)url.openConnection();
  33. if(200==urlCon.getResponseCode()){
  34. InputStream is = urlCon.getInputStream();
  35. InputStreamReader isr = new InputStreamReader(is,"utf-8");
  36. BufferedReader br = new BufferedReader(isr);
  37. String str = null;
  38. while((str = br.readLine())!=null){
  39. buffer.append(str);
  40. }
  41. br.close();
  42. isr.close();
  43. is.close();
  44. res = buffer.toString();
  45. //JsonParser parse =new JsonParser();
  46. object = JSONObject.fromObject(res);
  47. return object;
  48. }
  49. }catch(IOException e){
  50. e.printStackTrace();
  51. }
  52. //return object;
  53. return null;
  54. }
  55. public static JsonObject postDownloadJson(String path,String post){
  56. URL url = null;
  57. try {
  58. url = new URL(path);
  59. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  60. httpURLConnection.setRequestMethod("POST");// 提交模式
  61. // conn.setConnectTimeout(10000);//连接超时 单位毫秒
  62. // conn.setReadTimeout(2000);//读取超时 单位毫秒
  63. // 发送POST请求必须设置如下两行
  64. httpURLConnection.setDoOutput(true);
  65. httpURLConnection.setDoInput(true);
  66. // 获取URLConnection对象对应的输出流
  67. PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
  68. // 发送请求参数
  69. printWriter.write(post);//post的参数 xx=xx&yy=yy
  70. // flush输出流的缓冲
  71. printWriter.flush();
  72. //开始获取数据
  73. BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
  74. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  75. int len;
  76. byte[] arr = new byte[1024];
  77. while((len=bis.read(arr))!= -1){
  78. bos.write(arr,0,len);
  79. bos.flush();
  80. }
  81. bos.close();
  82. JsonParser parse = new JsonParser();
  83. return (JsonObject)parse.parse(bos.toString("utf-8"));
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. }
  87. return null;
  88. }
  89. //测试
  90. public static void main(String args [] ) {
  91. System.out.println("开始啦");
  92. //获取返回json对象
  93. JSONObject object = getXpath("https://api.bzqll.com/music/netease/songList?key=579621905&id=3778678&limit=10&offset=0");
  94. /**
  95. * {"result":"SUCCESS",
  96. * "code":200,
  97. * "data":
  98. * {
  99. * "songListId":"3778678",
  100. * "songListName":"云音乐热歌榜",
  101. * "songListPic":"https://p2.music.126.net/GhhuF6Ep5Tq9IEvLsyCN7w==/18708190348409091.jpg?param=300y300","songListCount":200,"songListPlayCount":2880181248,"songListDescription":"云音乐热歌榜:云音乐用户一周内收听所有线上歌曲 官方TOP排行榜,每周四更新。","songListUserId":1,
  102. * "songs":
  103. * [
  104. * {"id":"574566207",
  105. * "name":"盗将行","
  106. * singer":"花粥/马雨阳",
  107. * "pic":"https://api.bzqll.com/music/netease/pic?id=574566207&imgSize=300&key=579621905",
  108. * "lrc":"https://api.bzqll.com/music/netease/lrc?id=574566207&key=579621905",
  109. * "url":"https://api.bzqll.com/music/netease/url?id=574566207&key=579621905",
  110. * "time":198},
  111. * {"id":"574566207",
  112. * "name":"盗将行","
  113. * singer":"花粥/马雨阳",
  114. * "pic":"https://api.bzqll.com/music/netease/pic?id=574566207&imgSize=300&key=579621905",
  115. * "lrc":"https://api.bzqll.com/music/netease/lrc?id=574566207&key=579621905",
  116. * "url":"https://api.bzqll.com/music/netease/url?id=574566207&key=579621905",
  117. * "time":198}
  118. *
  119. * ]
  120. **/
  121. JSONArray array = object.getJSONObject("data").getJSONArray("songs");//获取json数组
  122. /**
  123. * 第一个参数为需转的json数组,第二个参数为对应实体类,
  124. */
  125. List<music> list = (List)JSONArray.toList(array, new music(), new JsonConfig());
  126. music m = list.get(20);
  127. System.out.println(object);
  128. System.out.println(m.getLrc());
  129. }
  130. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/618581
推荐阅读
相关标签
  

闽ICP备14008679号