小案例Josn字串的生成
寫了一個安卓的線上播放各個電視臺的小demo,放在手機和電視上都能看,但資源寫在string.xml裡感覺挺low
還是轉成json吧,維護起來方便些,或放在服務端也挺好,一個一個修改感覺好枯燥,就用程式碼吧
其實這個算很簡單的,只是藉此複習一下 流的操作
和 字串分割
以及 json的生成
和 json的解析
額外說一點:這裡的播放源目前都是可用的哦,使用直播流的軟體就能看相應的電視臺了
本文任務是把下列data.txt解析成json格式:如
{ "name": "CCTV1 綜合HD", "url": "http://112.50.243.7/PLTV/88888888/224/3221226489/index.m3u8", "mdTime": "2018-10-22" },

張風捷特烈.png
data.txt
<item>CCTV1 綜合HD★http://112.50.243.7/PLTV/88888888/224/3221226489/index.m3u8</item> <item>CCTV2 財經HD★http://112.50.243.7/PLTV/88888888/224/3221226554/index.m3u8</item> <item>CCTV-3 綜藝HD★http://183.207.249.5/PLTV/3/224/3221225588/index.m3u8</item> <item>CCTV4 中文國際★http://112.50.243.7/PLTV/88888888/224/3221226511/index.m3u8</item> <item>CCTV-5 體育HD★http://183.207.249.15/PLTV/3/224/3221225604/index.m3u8</item> <item>CCTV-6 電影HD★http://183.207.249.15/PLTV/3/224/3221225548/index.m3u8</item> <item>CCTV-7 軍事與農業HD★http://183.207.249.15/PLTV/3/224/3221225546/index.m3u8</item> <item>CCTV8 電視劇HD★http://223.110.245.167/ott.js.chinamobile.com/PLTV/3/224/3221227204/index.m3u8</item> <item>CCTV9 記錄HD★http://112.50.243.7/PLTV/88888888/224/3221226566/index.m3u8</item> <item>CCTV10 科教HD★http://112.50.243.7/PLTV/88888888/224/3221226488/index.m3u8</item> <item>CCTV11 戲曲HD★http://112.50.243.7/PLTV/88888888/224/3221226493/index.m3u8</item> <item>CCTV12 HD★http://112.50.243.7/PLTV/88888888/224/3221226623/index.m3u8</item> <item>CCTV13 新聞HD★http://112.50.243.7/PLTV/88888888/224/3221226507/index.m3u8</item> <item>CCTV14 少兒HD★http://112.50.243.7/PLTV/88888888/224/3221226497/index.m3u8</item> <item>CCTV-15 音樂★http://183.207.249.7/PLTV/3/224/3221225568/index.m3u8</item> <item>安徽衛視HD★http://183.207.249.15/PLTV/3/224/3221225634/index.m3u8</item> <item>北京衛視HD★http://183.207.249.7/PLTV/3/224/3221225574/index.m3u8</item> <item>湖南衛視 HD★http://223.110.243.173/PLTV/3/224/3221227220/index.m3u8</item> <item>浙江衛視 HD★http://223.110.243.173/PLTV/3/224/3221227215/index.m3u8</item> <item>青海衛視★http://183.207.249.7/PLTV/3/224/3221225580/index.m3u8</item> <item>山東衛視★http://183.207.249.7/PLTV/3/224/3221225586/index.m3u8</item> <item>江西衛視★http://183.207.249.15/PLTV/3/224/3221225537/index.m3u8</item> <item>兵團衛視HD★http://v.btzx.com.cn:1935/live/weishi.stream/playlist.m3u8</item> <item>東方衛視HD★http://111.48.34.182/huaweicdn.hb.chinamobile.com/PLTV/88888888/224/3221225810/1.m3u8?icpid=88888888 from=1 hms_devid=1143</item> <item>河北衛視★http://183.207.249.7/PLTV/3/224/3221225602/index.m3u8</item> <item>新疆衛視★http://183.207.249.15/PLTV/3/224/3221225523/index.m3u8</item> <item>四川衛視★http://183.207.249.15/PLTV/3/224/3221225570/index.m3u8</item> <item>西藏衛視★http://183.207.249.15/PLTV/3/224/3221225579/index.m3u8</item> <item>愛情喜劇★http://183.207.249.15/PLTV/3/224/3221225525/index.m3u8</item> <item>武林外傳★http://dlhls.cdn.zhanqi.tv/zqlive/33765_6qakZ.m3u8</item> <item>CNTV古裝劇場★http://183.207.249.7/PLTV/3/224/3221225527/index.m3u8</item> <item>動畫王國HD★http://183.207.249.15/PLTV/3/224/3221225555/index.m3u8</item> <item>海外劇場★http://183.207.249.15/PLTV/3/224/3221225547/index.m3u8</item> <item>完美遊戲★http://183.207.249.15/PLTV/3/224/3221225539/index.m3u8</item> <item>電影輪播★http://112.50.243.7/PLTV/88888888/224/3221226670/index.m3u8</item> <item>金鷹紀實HD★http://112.50.243.7/PLTV/88888888/224/3221226613/index.m3u8</item>
一、生成json字串
0思路步驟:
1).新建一個maven管理的java專案,引入gson依賴
2).用檔案讀流+bufferd包裝類一行一行讀取data.txt,對每行的字串切割
3).建一個實體類,每讀一行將資料載入如實體類
4).將實體類集合輸出成Json格式字串,並通過檔案寫流+bufferd寫出
1.新建一個maven管理的java專案,引入gson依賴
<!-- 為簡化set,get,toString程式碼引入lombok --> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> <scope>provided</scope> </dependency> <!--gson依賴--> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency>
2.檔案操作(小demo,異常直接拋了)
public class OnlineVideo { public static void main(String[] args) throws IOException { //讀取檔案 String path = "I:\\Java\\SpringBoot\\small_project\\src\\main\\resources\\data.txt"; BufferedReader br = new BufferedReader(new FileReader(path)); ArrayList<VideoBean> videoBeans = new ArrayList<VideoBean>(); String line = ""; while ((line = br.readLine()) != null) { String res = line.split(">")[1].split("<")[0]; VideoBean videoBean = new VideoBean(); videoBean.setName(res.split("★")[0]); videoBean.setUrl(res.split("★")[1]); videoBean.setMdTime(new SimpleDateFormat("YYYY-MM-dd").format(System.currentTimeMillis())); videoBeans.add(videoBean); } br.close(); String json = new GsonBuilder().create().toJson(videoBeans); String target = "I:\\Java\\SpringBoot\\small_project\\src\\main\\resources\\data.json"; FileWriter fileWriter = new FileWriter(target); fileWriter.write(json); fileWriter.close(); } }
3.data.json
[ { "name": "CCTV1 綜合HD", "url": "http://112.50.243.7/PLTV/88888888/224/3221226489/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV2 財經HD", "url": "http://112.50.243.7/PLTV/88888888/224/3221226554/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV-3 綜藝HD", "url": "http://183.207.249.5/PLTV/3/224/3221225588/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV4 中文國際", "url": "http://112.50.243.7/PLTV/88888888/224/3221226511/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV-5 體育HD", "url": "http://183.207.249.15/PLTV/3/224/3221225604/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV-6 電影HD", "url": "http://183.207.249.15/PLTV/3/224/3221225548/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV-7 軍事與農業HD", "url": "http://183.207.249.15/PLTV/3/224/3221225546/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV8 電視劇HD", "url": "http://223.110.245.167/ott.js.chinamobile.com/PLTV/3/224/3221227204/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV9 記錄HD", "url": "http://112.50.243.7/PLTV/88888888/224/3221226566/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV10 科教HD", "url": "http://112.50.243.7/PLTV/88888888/224/3221226488/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV11 戲曲HD", "url": "http://112.50.243.7/PLTV/88888888/224/3221226493/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV12 HD", "url": "http://112.50.243.7/PLTV/88888888/224/3221226623/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV13 新聞HD", "url": "http://112.50.243.7/PLTV/88888888/224/3221226507/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV14 少兒HD", "url": "http://112.50.243.7/PLTV/88888888/224/3221226497/index.m3u8", "mdTime": "2018-10-22" }, { "name": "CCTV-15 音樂", "url": "http://183.207.249.7/PLTV/3/224/3221225568/index.m3u8", "mdTime": "2018-10-22" }, { "name": "安徽衛視HD", "url": "http://183.207.249.15/PLTV/3/224/3221225634/index.m3u8", "mdTime": "2018-10-22" }, { "name": "北京衛視HD", "url": "http://183.207.249.7/PLTV/3/224/3221225574/index.m3u8", "mdTime": "2018-10-22" }, { "name": "湖南衛視 HD", "url": "http://223.110.243.173/PLTV/3/224/3221227220/index.m3u8", "mdTime": "2018-10-22" }, { "name": "浙江衛視 HD", "url": "http://223.110.243.173/PLTV/3/224/3221227215/index.m3u8", "mdTime": "2018-10-22" }, { "name": "青海衛視", "url": "http://183.207.249.7/PLTV/3/224/3221225580/index.m3u8", "mdTime": "2018-10-22" }, { "name": "山東衛視", "url": "http://183.207.249.7/PLTV/3/224/3221225586/index.m3u8", "mdTime": "2018-10-22" }, { "name": "江西衛視", "url": "http://183.207.249.15/PLTV/3/224/3221225537/index.m3u8", "mdTime": "2018-10-22" }, { "name": "兵團衛視HD", "url": "http://v.btzx.com.cn:1935/live/weishi.stream/playlist.m3u8", "mdTime": "2018-10-22" }, { "name": "東方衛視HD", "url": "http://111.48.34.182/huaweicdn.hb.chinamobile.com/PLTV/88888888/224/3221225810/1.m3u8?icpid\u003d88888888\u0026#038;from\u003d1\u0026#038;hms_devid\u003d1143", "mdTime": "2018-10-22" }, { "name": "河北衛視", "url": "http://183.207.249.7/PLTV/3/224/3221225602/index.m3u8", "mdTime": "2018-10-22" }, { "name": "新疆衛視", "url": "http://183.207.249.15/PLTV/3/224/3221225523/index.m3u8", "mdTime": "2018-10-22" }, { "name": "四川衛視", "url": "http://183.207.249.15/PLTV/3/224/3221225570/index.m3u8", "mdTime": "2018-10-22" }, { "name": "西藏衛視", "url": "http://183.207.249.15/PLTV/3/224/3221225579/index.m3u8", "mdTime": "2018-10-22" }, { "name": "愛情喜劇", "url": "http://183.207.249.15/PLTV/3/224/3221225525/index.m3u8", "mdTime": "2018-10-22" }, { "name": "武林外傳", "url": "http://dlhls.cdn.zhanqi.tv/zqlive/33765_6qakZ.m3u8", "mdTime": "2018-10-22" }, { "name": "CNTV古裝劇場", "url": "http://183.207.249.7/PLTV/3/224/3221225527/index.m3u8", "mdTime": "2018-10-22" }, { "name": "動畫王國HD", "url": "http://183.207.249.15/PLTV/3/224/3221225555/index.m3u8", "mdTime": "2018-10-22" }, { "name": "海外劇場", "url": "http://183.207.249.15/PLTV/3/224/3221225547/index.m3u8", "mdTime": "2018-10-22" }, { "name": "完美遊戲", "url": "http://183.207.249.15/PLTV/3/224/3221225539/index.m3u8", "mdTime": "2018-10-22" }, { "name": "電影輪播", "url": "http://112.50.243.7/PLTV/88888888/224/3221226670/index.m3u8", "mdTime": "2018-10-22" }, { "name": "金鷹紀實HD", "url": "http://112.50.243.7/PLTV/88888888/224/3221226613/index.m3u8", "mdTime": "2018-10-22" } ]
二、安卓中的使用
可以放在伺服器上,用網路訪問,這裡就放在assets目錄下測試
新增依賴 implementation 'com.google.code.gson:gson:2.2.4'
1.json解析的實體類
/** * 作者:張風捷特烈<br/> * 時間:2018/10/22 0022:21:03<br/> * 郵箱:[email protected]<br/> * 說明:json解析的實體類 */ public class VideoBean { /** * name : CCTV1 綜合HD * url : http://112.50.243.7/PLTV/88888888/224/3221226489/index.m3u8 * mdTime : 2018-10-22 */ private String name; private String url; private String mdTime; public static VideoBean objectFromData(String str) { return new Gson().fromJson(str, VideoBean.class); } public static List<VideoBean> arrayVideoBeanFromData(String str) { Type listType = new TypeToken<ArrayList<VideoBean>>() { }.getType(); return new Gson().fromJson(str, listType); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getMdTime() { return mdTime; } public void setMdTime(String mdTime) { this.mdTime = mdTime; }
2.讀取data.json,解析成實體類集合
String json = FileHelper.get().readInAssets(this, "data.json"); List<VideoBean> videoBeans = VideoBean.arrayVideoBeanFromData(json);
3.用listView展示資料:
mIdLvVideo.setAdapter(new MyLVAdapter<VideoBean>(this, videoBeans, R.layout.item_text_only) { @Override public void setData(MyLVHolder holder, VideoBean data, int position) { holder.setText(R.id.id_tv_name, data.getName()); } });

listView.png
後記:捷文規範
1.本文成長記錄及勘誤表
專案原始碼 | 日期 | 備註 |
---|---|---|
V0.1--無 | 2018-10-22 | ofollow,noindex">小案例Josn字串的生成 |
2.更多關於我
筆名 | 微信 | 愛好 | |
---|---|---|---|
張風捷特烈 | 1981462002 | zdl1994328 | 語言 |
我的github | 我的簡書 | 我的CSDN | 個人網站 |
3.宣告
1----本文由張風捷特烈原創,轉載請註明
2----歡迎廣大程式設計愛好者共同交流
3----個人能力有限,如有不正之處歡迎大家批評指證,必定虛心改正
4----看到這裡,我在此感謝你的喜歡與支援